Error 401 in C#

不羁岁月 提交于 2020-01-03 02:26:20

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!