【问题标题】:WebException: The underlying connection was closed: An unexpected error occurred on a sendWebException:底层连接已关闭:发送时发生意外错误
【发布时间】:2018-02-02 22:20:53
【问题描述】:

我们正在开发一个与肥皂网络服务通信的 .Net 应用程序(为 .net 4.0 编译)。

在检索 http Web 响应内容时,该应用程序在多台机器上运行良好。但是,在其中一台机器(Windows Server 2008 R2)上,应用程序无法检索响应。这是一个代码示例:

HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://200.57.3.82:3443/AdministradorQr/WebServiceDodaPort?wsdl");
request.Method = "GET";
WebResponse response;
Stream responseStream = response.GetResponseStream();
string result = string.Empty;
using (StreamReader streamReader = new StreamReader(responseStream))
{
   result = streamReader.ReadToEnd();
}

我们得到的异常是:

The underlying connection was closed: An unexpected error occurred on a send. System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a send. ---> System.IO.IOException: Unable to read data from the transport connection: An established connection was aborted by the software in your host machine. ---> System.Net.Sockets.SocketException: An established connection was aborted by the software in your host machine
   at System.Net.Sockets.Socket.Receive(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags)
   at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
   --- End of inner exception stack trace ---
   at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
   at System.Net.FixedSizeReader.ReadPacket(Byte[] buffer, Int32 offset, Int32 count)
   at System.Net.Security.SslState.StartReceiveBlob(Byte[] buffer, AsyncProtocolRequest asyncRequest)
   at System.Net.Security.SslState.CheckCompletionBeforeNextReceive(ProtocolToken message, AsyncProtocolRequest asyncRequest)
   at System.Net.Security.SslState.StartSendBlob(Byte[] incoming, Int32 count, AsyncProtocolRequest asyncRequest)
   at System.Net.Security.SslState.ForceAuthentication(Boolean receiveFirst, Byte[] buffer, AsyncProtocolRequest asyncRequest)
   at System.Net.Security.SslState.ProcessAuthentication(LazyAsyncResult lazyResult)
   at System.Net.TlsStream.CallProcessAuthentication(Object state)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
   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)
   --- End of inner exception stack trace ---
   at System.Net.HttpWebRequest.GetResponse()

问题是,使用具有不同 URL 的相同代码,我们得到了成功的响应,并且问题只发生在一台机器上,即使有另一台机器也使用 Windows 2008 工作得很好。

目前我们无法升级 .net 框架以尝试使用在某些地方找到的解决方案,例如使用 TLS 1.2:"The underlying connection was closed: An unexpected error occurred on a send." With SSL Certificate

正如我所提到的,同样的代码在使用相同 Windows/.net 版本的其他机器上也能正常工作是很奇怪的,而且对于像 http://www.yahoo.com 这样的其他 URL 也能正常工作也很奇怪

【问题讨论】:

  • 这台机器上是否安装了杀毒软件?禁用后可以试试吗?
  • 我们已安装 McAfee...我们禁用了实时保护并进行了测试,但仍然遇到相同的异常
  • 我们也尝试了.net framework 4.0的解决方案,使用ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;但是,如果我们使用它,我们会收到一条错误消息,指出不支持指定的协议

标签: c# httpwebrequest webexception


【解决方案1】:

不确定它是否可以帮助您,但您可以尝试在“Global.asax -> Application_Start”函数中添加以下行。看看它的工作原理。基本上这段代码会强制你的应用只使用 TLS1.2,你不需要升级你的框架。

“System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12;”

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-02
    • 2015-11-22
    • 2015-07-05
    • 1970-01-01
    相关资源
    最近更新 更多