问题
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?
回答1:
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.
回答2:
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
来源:https://stackoverflow.com/questions/11608843/error-401-in-c-sharp