远程服务器未经授权地返回错误(401)。 c#httpwebrequest [英] the remote server returned an error (401) unauthorized. httpwebrequest in c#

查看:1400
本文介绍了远程服务器未经授权地返回错误(401)。 c#httpwebrequest的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的所有人,

Dear All,

HttpWebRequest request = WebRequest.Create("url") as HttpWebRequest;
request.Method = "GET";
request.Credentials = new NetworkCredential("veera-vtw", "veera");
//  the next line generates exception [System.Net.WebException] = {"The remote server returned an error: (401) Unauthorized."}
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{
  // Get the response stream  
  StreamReader reader = new StreamReader(response.GetResponseStream());

  // Console application output  
  Console.WriteLine(reader.ReadToEnd());
}

推荐答案

http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html [ ^ ]

说:

http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html[^]
says:
Quote:

10.4.2 401 Unauthorized



请求需要用户身份验证。响应必须包含WWW-Authenticate头字段(第14.47节),其中包含适用于所请求资源的质询。客户端可以使用合适的Authorization头字段重复请求(第14.8节)。如果请求已包含授权凭据,则401响应表示已拒绝授权这些凭据。如果401响应包含与先前响应相同的挑战,并且用户代理已经尝试过至少一次认证,则应该向用户呈现响应中给出的实体,因为该实体可能包括相关的诊断信息。 HTTP访问身份验证在HTTP身份验证:基本和摘要访问身份验证[43]中进行了解释。

10.4.2 401 Unauthorized

The request requires user authentication. The response MUST include a WWW-Authenticate header field (section 14.47) containing a challenge applicable to the requested resource. The client MAY repeat the request with a suitable Authorization header field (section 14.8). If the request already included Authorization credentials, then the 401 response indicates that authorization has been refused for those credentials. If the 401 response contains the same challenge as the prior response, and the user agent has already attempted authentication at least once, then the user SHOULD be presented the entity that was given in the response, since that entity might include relevant diagnostic information. HTTP access authentication is explained in "HTTP Authentication: Basic and Digest Access Authentication" [43].



此外,请不要发布您的用户名和密码。


Moreover please do not post your user names and passwords.

这篇关于远程服务器未经授权地返回错误(401)。 c#httpwebrequest的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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