WebException:基础连接已关闭:发送时发生意外错误

时间:2018-02-02 22:20:53

标签: c# httpwebrequest webexception

我们正在开发一个与肥皂网服务通信的.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这样的其他网站也可以正常工作

1 个答案:

答案 0 :(得分:0)

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

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