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?

abatishchev
  • 98,240
  • 88
  • 296
  • 433
  • Check here http://en.wikipedia.org/wiki/List_of_HTTP_status_codes It seems an issue related to the authorization of the web server! – iSamnium Jul 23 '12 at 08: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. – James Jul 23 '12 at 08:37

2 Answers2

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.

Community
  • 1
  • 1
James
  • 80,725
  • 18
  • 167
  • 237
  • Notice the use of the using statement, which causes the client to get automatically disposed when it leaves the scope. – Liron Jul 23 '12 at 08:38
  • 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 ^^' – Guillaume Fortunier Jul 23 '12 at 08:47
  • @GuillaumeFortunier did you find the `TransportSettings` property? – James Jul 23 '12 at 10:21
  • 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("http://ws.jeuxvideo.com/search/mario%20kart" + TitleNewsGrid.Text); but block at Fond.GetStreamAsync – Guillaume Fortunier Jul 23 '12 at 11:30
  • Sounds like you may be hitting a deadlock in your code. Try the solution provided [here](http://stackoverflow.com/questions/9895048/async-call-with-await-in-httpclient-never-returns) and I would recommend reading the answer posted [here](http://stackoverflow.com/questions/10343632/httpclient-getasync-never-returns-when-using-await-async/10351400#10351400) as well. – James Jul 23 '12 at 11:37
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

JohnnBlade
  • 4,261
  • 1
  • 21
  • 22