【问题标题】:WebRequest: The underlying connection was closedWebRequest:底层连接已关闭
【发布时间】:2017-09-21 05:37:56
【问题描述】:

我的应用程序必须下载一个特定的文件,here is the url。该应用程序在提供者方面最近发生变化后运行良好多年,我得到“底层连接已关闭:发送时发生意外错误。”

我已阅读网络中的所有相关信息,但没有一个建议的修复对我有用。

重要提示:该代码适用于 Net Framework 4.6,但我需要它适用于 3.5。

有什么想法吗?

代码如下:

        var url = new Uri(@"http://www.ezv.admin.ch/pdf_linker.php?doc=edecReceiptResponse_stylesheet_v_3_0");

        var request = (HttpWebRequest)WebRequest.Create(url);
        request.KeepAlive = false;
        request.AllowAutoRedirect = true;
        request.UserAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36";
        request.ProtocolVersion = HttpVersion.Version10;

更新:堆栈:

System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a send. ---> System.IO.IOException: Received an unexpected EOF or 0 bytes from the transport stream.
   at System.Net.FixedSizeReader.ReadPacket(Byte[] buffer, Int32 offset, Int32 count)
   at System.Net.Security.SslState.StartReadFrame(Byte[] buffer, Int32 readBytes, AsyncProtocolRequest asyncRequest)
   at System.Net.Security.SslState.StartReceiveBlob(Byte[] buffer, AsyncProtocolRequest asyncRequest)
   at System.Net.Security.SslState.ForceAuthentication(Boolean receiveFirst, Byte[] buffer, AsyncProtocolRequest asyncRequest)
   at System.Net.Security.SslState.ProcessAuthentication(LazyAsyncResult lazyResult)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Net.TlsStream.ProcessAuthentication(LazyAsyncResult result)
   at System.Net.TlsStream.Write(Byte[] buffer, Int32 offset, Int32 size)
   at System.Net.PooledStream.Write(Byte[] buffer, Int32 offset, Int32 size)
   at System.Net.ConnectStream.WriteHeaders(Boolean async)

还有一个更新:与 Fiddler 一起检查连接到 httpS 的应用,并将收到的响应与 net 4.6(有效)和 net 3.5(失败)进行比较:

  • 4.6 收到两个响应,5670 + 664785 字节
  • 3.5 还收到两个响应,每个响应 7 个字节

【问题讨论】:

  • www.ezv.admin.ch 关闭连接或者您有通信问题(网络问题/防火墙)。
  • 显示其堆栈跟踪
  • 请显示来自 Fiddler 或 Wireshark 的 HTTP 请求/响应。
  • @Eugene Komisarenko,看起来服务器没有响应,服务器只是关闭了连接。 Fiddler 只显示请求。
  • 也许你正在处理新版本的安全协议,试试这个设置stackoverflow.com/a/12244894/920557

标签: c# .net


【解决方案1】:

根据test 进行,这里是 www.ezv.admin.ch 支持的协议。

Protocols
TLS 1.2 Yes
TLS 1.1 No
TLS 1.0 No
SSL 3   No
SSL 2   No

服务器端升级很可能已将安全协议的版本升级为专有 TLS 1.2。

根据以下article.NET Framework 3.5 不支持此版本,您唯一的选择是升级您的客户端库或修补它们,见下文。

.NET 3.5 或更低版本。不支持 TLS 1.2 (*) 并且没有 解决方法。将您的应用程序升级到更新版本的 框架。

以下是有关如何修补客户端以添加支持的更多信息。

附言正如下面提到的来自 Microsoft 的 Christian Pop,有一个 .NET 3.5 的最新补丁支持 TLS1.2。

见:

KB3154​​518 – 可靠性汇总 HR-1605 – NDP 2.0 SP2 – Win7 SP1/Win 2008 R2 SP1 KB3154​​519 – 可靠性汇总 HR-1605 – NDP 2.0 SP2 – Win8 RTM/Win 2012 RTM KB3154​​520 – 可靠性汇总 HR-1605 – NDP 2.0 SP2 – Win8.1RTM/Win 2012 R2 RTM KB3156421 -1605 HotFix Rollup through 适用于 Windows 10 的 Windows 更新。

【讨论】:

  • 好的。现在一切都清楚了。非常感谢您的帮助。
猜你喜欢
  • 2020-12-30
  • 1970-01-01
  • 2013-10-31
  • 2018-01-31
  • 2017-09-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多