错误401在C#错误

2023-09-07 09:06:55 作者:拥有后的遗弃

我的工作我的第一个应用程序适用于Windows 8,但我有一个问题,当我启动我的应用程序,我已经得到了错误响应状态code并不表明成功:

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(所需授权)

所以,是的,我需要包括用户名和密码,但我不知道在我的code:

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);    

那么,我包括在code中的用户名和密码?

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

推荐答案

应该有一个证书 TransportSettings 例如

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

如果你没有访问 TransportSettings 属性,你需要使用 HttpClientHandler 代替。在复制code没点,已经有一个很好的例子here.

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.