【问题标题】:Authentication failed during request.GetResponse() in C# using HttpWebRequest使用 HttpWebRequest 在 C# 中的 request.GetResponse() 期间身份验证失败
【发布时间】: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 中的所有引用:

Authentication failed because the remote party has closed the transport stream exception when getting a response from webservice

Failure on HttpWebrequest with inner exception Authentication failed because the remote party has closed the transport stream

但没有用,仍然出现同样的错误。我的代码有问题吗?

【问题讨论】:

    标签: c# asp.net authentication httpwebrequest httpwebresponse


    【解决方案1】:

    修改 ServicePointManager.SecurityProtocol 后这个问题解决了:

    ServicePointManager.SecurityProtocol = (SecurityProtocolType)768 | (SecurityProtocolType)3072;
    

    由于 Tls 1.1、1.2 在 System.Net 中直接在 .net framework 4.0 中不可用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-11-02
      • 2018-10-11
      • 2011-09-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-24
      • 1970-01-01
      相关资源
      最近更新 更多