C#中的错误401 [英] Error 401 in C#
问题描述
401(需要授权)
所以是的,我需要包括用户名和密码,但我不知道我的代码在哪里:
var fond = new HttpClient();
var reponse = await fond.GetStreamAsync(http://example.com/search/mario%20kart+ TitleNewsGrid.Text);
所以我在代码中包含用户名和密码?
Credentials
属性在 TransportSettings
中,例如
using(var client = new HttpClient())
{
client.TransportSettings.Credentials = new System.Net.NetworkCredential(username,pwd);
...
}
如果你没有访问 TransportSettings
属性,您将需要使用 HttpClientHandler
。没有重复的代码,已经有一个很好的例子这里。
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?
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.
这篇关于C#中的错误401的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!