ASP Net“底层连接已关闭:发送时发生意外错误。”

面临一个很奇怪的问题。生产服务器上的程序向 googleapis 发送 get 请求。我收到错误“底层连接已关闭:发送时发生意外错误。”

操作系统:Windows Server 2008 R2 SP1

IIS 7

启用 TLS 1.0/1.1./1.2、SSL 2.0/3.0

.Net 框架 4.8

URL“https://googleapi.com”添加到“受信任的站点”列表

此 URL 在 IE 中打开。它可以在正常工作的 PC(以及我所有其他 PC)上正常工作。问题仅在生产服务器上。

也许有人遇到过这样的事情,并会告诉我在这种情况下在哪里可以找到解决方案?

测试代码:

string url = $"https://www.googleapis.com/analytics/v3/data/ga";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "GET";

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

using (WebResponse response = request.GetResponse())
{
    using (Stream responseStream = response.GetResponseStream())
    {
        StreamReader reader = new StreamReader(responseStream);
        string result = reader.ReadToEnd();
        reader.Close();
        Console.WriteLine($"nn{result}nn");
    }
}

例外:

System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a send. ---> Syste
m.IO.IOException: Unable to read data from the transport connection: An existing connection was forcibly closed by the r
emote host. ---> System.Net.Sockets.SocketException: An existing connection was forcibly closed by the remote host
   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 asyncReq
uest)
   at System.Net.Security.SslState.ProcessReceivedBlob(Byte[] buffer, Int32 count, AsyncProtocolRequest asyncRequest)
   at System.Net.Security.SslState.StartReceiveBlob(Byte[] buffer, AsyncProtocolRequest asyncRequest)
   at System.Net.Security.SslState.CheckCompletionBeforeNextReceive(ProtocolToken message, AsyncProtocolRequest asyncReq
uest)
   at System.Net.Security.SslState.ProcessReceivedBlob(Byte[] buffer, Int32 count, AsyncProtocolRequest asyncRequest)
   at System.Net.Security.SslState.StartReceiveBlob(Byte[] buffer, AsyncProtocolRequest asyncRequest)
   at System.Net.Security.SslState.CheckCompletionBeforeNextReceive(ProtocolToken message, AsyncProtocolRequest asyncReq
uest)
   at System.Net.Security.SslState.ForceAuthentication(Boolean receiveFirst, Byte[] buffer, AsyncProtocolRequest asyncRe
quest)
   at System.Net.Security.SslState.ProcessAuthentication(LazyAsyncResult lazyResult)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object
state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, B
oolean preserveSyncCtx)
   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()
   at GoogleFetchTest.Program.Main(String[] args)
stack overflow ASP Net "The underlying connection was closed: An unexpected error occurred on a send."
原文答案

答案:

作者头像

谢谢大家!在 Internet 选项中包含 SSL 2.0 / 3.0 并将 URL“https://www.gogleapis.com”添加到受信任的站点对我有所帮助

Internet Option Sreenshot

作者头像

这是与协议(TLS 协议)相关的问题。在调用服务之前使用以下解决方案应该可以解决问题,

ServicePointManager.SecurityProtocol |= SecurityProtocolType.Tls12