【问题标题】:Another Failure on HttpWebrequest with inner exception Authentication failed because the remote party has closed the transport streamHttpWebrequest 上的另一个失败,内部异常身份验证失败,因为远程方已关闭传输流
【发布时间】:2016-05-07 08:36:29
【问题描述】:

我知道以前有人问过这个问题,但在这些情况下建议的解决方案对我不起作用。我有一个简单的控制台应用程序,可以从网站上抓取数据。昨晚他们更新了他们的安全设置,所以我的代码不再工作了。

这是我尝试使用的代码 sn-p:

try
{
    ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls;
    var request2 = WebRequest.Create("https://www.eex-transparency.com/") as HttpWebRequest;
    request2.Method = "GET";
    request2.Accept = "text/html, application/xhtml+xml, */*";
    request2.Headers.Set(HttpRequestHeader.AcceptLanguage, "en-gb");
    request2.Headers.Set(HttpRequestHeader.AcceptEncoding, "gzip, deflate");
    request2.Headers.Add("DNT", "1");
    request2.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko";
    request2.Host = "www.eex-transparency.com";
    request2.KeepAlive = true;
    request2.Headers.Set(HttpRequestHeader.CacheControl, "no-cache");
    request2.Proxy = ProxyFactory.GetWebProxy();
    using (var response = request2.GetResponse())
    {}
}
catch (Exception ex)
{ Console.WriteLine(ex);}

我收到的错误是:

System.Net.WebException:底层连接已关闭:发送时发生意外错误。 ---> System.IO.IOException: 身份验证失败,因为远程方已关闭传输流。

在 System.Net.Security.SslState.StartReadFrame(Byte[] buffer, Int32 readBytes, AsyncProtocolRequest asyncRequest)

在 System.Net.Security.SslState.StartReceiveBlob(Byte[] 缓冲区,AsyncProtocol 请求异步请求)

在 System.Net.Security.SslState.StartSendBlob(Byte[] 传入,Int32 计数,A syncProtocolRequest asyncRequest)

在 System.Net.Security.SslState.ForceAuthentication(Boolean receiveFirst, Byte[] buffer, AsyncProtocolRequest asyncRequest)

在 System.Net.Security.SslState.ProcessAuthentication(LazyAsyncResultlazyResult)

在 System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)

at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)

在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)

在 System.Net.TlsStream.ProcessAuthentication(LazyAsyncResult 结果)

在 System.Net.TlsStream.Write(Byte[] 缓冲区,Int32 偏移量,Int32 大小)

在 System.Net.ConnectStream.WriteHeaders(布尔异步)

---内部异常堆栈跟踪结束---

任何帮助将不胜感激。

提前致谢。

【问题讨论】:

  • 该 URL 仅支持 TLS v1.1 / v1.2,请尝试相应调整您的 SecurityProtocol 掩码
  • 感谢@AlexK。那行得通,对于我的教育,您是如何发现该 url 仅支持 v1.1/1.2 的?对于遇到此 tls11 和 tls12 的任何人都可以在框架 4.5 及更高版本中使用。
  • 欢迎您。我通过sslanalyzer.comodoca.com 运行了 URL
  • @Alex K. 好的,感谢我将来使用。
  • 如果你解决了这个问题,你可以发布你所做的作为答案然后接受它。

标签: c# ssl


【解决方案1】:

此特定网址仅支持 TLS 版本 1.1 和 1.2。添加以下代码以使代码正常工作:

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls11;

确保您使用的是 Framework 4.5 或更高版本

【讨论】:

  • 嗨@Elias,我有这个功能,请你指导我把这段代码放在哪里?我面临同样的问题。 TIA
  • @HardikMasalawala 将它放在您拥有 httpwebrequest 代码的位置,例如HttpWebRequest 请求 = (HttpWebRequest)WebRequest.Create(url); ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; var resp = req.GetResponse();
  • 感谢您的快速回复。
【解决方案2】:

做一件事会奏效

request2.KeepAlive = false;

【讨论】:

    猜你喜欢
    • 2013-11-07
    • 2015-08-20
    • 1970-01-01
    • 2013-03-24
    • 2017-08-18
    • 2022-06-29
    • 2015-09-28
    • 1970-01-01
    相关资源
    最近更新 更多