C#中的错误401 [英] Error 401 in C#

查看:930
本文介绍了C#中的错误401的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发我的第一个Windows 8应用程序,但我有一个问题,当我启动我的应用程序,我有错误响应状态代码不表示成功:


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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆