【发布时间】:2018-01-02 00:20:31
【问题描述】:
我正在使用 C#、.Net 框架 4.0 并尝试为 Http 基本身份验证执行 HttpWebRequest 并出现以下错误:
Inner Exception 1:
IOException: Authentication failed because the remote party has closed the transport stream.
我正在使用以下代码:
string svcCredentials = Convert.ToBase64String(System.Text.Encoding.GetEncoding("ISO-8859-1").GetBytes(userName + ":" + password));
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Headers.Add("Authorization", "Basic " + svcCredentials);
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls;
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
using (Stream stream = response.GetResponseStream())
using (StreamReader reader = new StreamReader(stream))
{
html = reader.ReadToEnd();
}
GetResponse() 期间出错。
我尝试了 SO 中的所有引用:
但没有用,仍然出现同样的错误。我的代码有问题吗?
【问题讨论】:
标签: c# asp.net authentication httpwebrequest httpwebresponse