1

I'm working on my first app for Windows 8 but I have a problem, when I'm launch my app, I've got the error "Response status code does not indicate success:

401 (Authorization Required)

So yes, I need to include username and password but I don't know where in my code:

var fond = new HttpClient();
var reponse = await fond.GetStreamAsync("http://example.com/search/mario%20kart" + TitleNewsGrid.Text);    

So where I'm include the username and password in the code?

2
  • Check here en.wikipedia.org/wiki/List_of_HTTP_status_codes It seems an issue related to the authorization of the web server! Commented Jul 23, 2012 at 8:33
  • @iSamnium The OP understands what the error is, they are asking how to pass credentials to the server not what the status code represents. Commented Jul 23, 2012 at 8:37

2 Answers 2

3

There should be a Credentials property in the TransportSettings e.g.

using (var client = new HttpClient())
{
    client.TransportSettings.Credentials = new System.Net.NetworkCredential("username", "pwd");
    ...
}

If you don't have access to the TransportSettings property, you will need to use HttpClientHandler instead. No point in duplicating code, there is already a good example here.

Sign up to request clarification or add additional context in comments.

5 Comments

Notice the use of the using statement, which causes the client to get automatically disposed when it leaves the scope.
Hey :) Thanks for your help. For the moment i use: System.Net.Http.HttpClient Fond = new HttpClient(); But TransportSettings don't use System.Net.Http.HttpClient... So I'm a little missed ^^'
@GuillaumeFortunier did you find the TransportSettings property?
private async void PremierTest() { using (var handler = new HttpClientHandler()) { handler.Credentials = new System.Net.NetworkCredential("appendr", "e32!cdf"); using (var client = new HttpClient(handler){}) { System.Net.Http.HttpClient Fond = new HttpClient(); var reponse = await Fond.GetStreamAsync("ws.jeuxvideo.com/search/mario%20kart" + TitleNewsGrid.Text); but block at Fond.GetStreamAsync
Sounds like you may be hitting a deadlock in your code. Try the solution provided here and I would recommend reading the answer posted here as well.
0

try this

client.TransportSettings.Credentials = new System.Net.NetworkCredential("username", "password");

for more info http://msdn.microsoft.com/en-us/library/system.net.http.httpclient%28v=vs.110%29.aspx

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.