C# &引用;现有连接被远程主机强制关闭;在SSL中连接到同一站点时
好的,对于我们的站点,我会说它是“”,是一个.NET MVC 4.5.1应用程序,我们有以下代码:C# &引用;现有连接被远程主机强制关闭;在SSL中连接到同一站点时,c#,asp.net-mvc,iis,ssl,C#,Asp.net Mvc,Iis,Ssl,好的,对于我们的站点,我会说它是“”,是一个.NET MVC 4.5.1应用程序,我们有以下代码: var http = (HttpWebRequest)WebRequest.Create("https://www.foo-bar.com/category.rss"); var response = http.GetResponse(); var stream = response.GetResponseStream(); var sr = new StreamReader(stream);
var http = (HttpWebRequest)WebRequest.Create("https://www.foo-bar.com/category.rss");
var response = http.GetResponse();
var stream = response.GetResponseStream();
var sr = new StreamReader(stream);
s = sr.ReadToEnd();
这在本地起作用,在“http”下在生产中起作用,但一旦转到https,就会出现以下错误:
{
"ClassName":"System.Net.WebException",
"Message":"The underlying connection was closed: An unexpected error occurred on a send.",
"Data":null,
"InnerException":{
"ClassName":"System.IO.IOException",
"Message":"Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host.",
"Data":null,
"InnerException":{
"NativeErrorCode":10054,
"ClassName":"System.Net.Sockets.SocketException",
"Message":"An existing connection was forcibly closed by the remote host",
"Data":null,
"InnerException":null,
"HelpURL":null,
"StackTraceString":" at System.Net.Sockets.Socket.Receive(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags)\r\n at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)",
"RemoteStackTraceString":null,
"RemoteStackIndex":0,
"ExceptionMethod":"8\nReceive\nSystem, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089\nSystem.Net.Sockets.Socket\nInt32 Receive(Byte[], Int32, Int32, System.Net.Sockets.SocketFlags)",
"HResult":-2147467259,
"Source":"System",
"WatsonBuckets":null
},
"HelpURL":null,
"StackTraceString":" at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)\r\n at System.Net.FixedSizeReader.ReadPacket(Byte[] buffer, Int32 offset, Int32 count)\r\n at System.Net.Security.SslState.StartReceiveBlob(Byte[] buffer, AsyncProtocolRequest asyncRequest)\r\n at System.Net.Security.SslState.CheckCompletionBeforeNextReceive(ProtocolToken message, AsyncProtocolRequest asyncRequest)\r\n at System.Net.Security.SslState.StartSendBlob(Byte[] incoming, Int32 count, AsyncProtocolRequest asyncRequest)\r\n at System.Net.Security.SslState.ForceAuthentication(Boolean receiveFirst, Byte[] buffer, AsyncProtocolRequest asyncRequest)\r\n at System.Net.Security.SslState.ProcessAuthentication(LazyAsyncResult lazyResult)\r\n at System.Net.TlsStream.CallProcessAuthentication(Object state)\r\n at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)\r\n at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)\r\n at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)\r\n at System.Net.TlsStream.ProcessAuthentication(LazyAsyncResult result)\r\n at System.Net.TlsStream.Write(Byte[] buffer, Int32 offset, Int32 size)\r\n at System.Net.PooledStream.Write(Byte[] buffer, Int32 offset, Int32 size)\r\n at System.Net.ConnectStream.WriteHeaders(Boolean async)",
"RemoteStackTraceString":null,
"RemoteStackIndex":0,
"ExceptionMethod":"8\nRead\nSystem, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089\nSystem.Net.Sockets.NetworkStream\nInt32 Read(Byte[], Int32, Int32)",
"HResult":-2146232800,
"Source":"System",
"WatsonBuckets":null
},
"HelpURL":null,
"StackTraceString":" at System.Net.HttpWebRequest.GetResponse()\r\n at ASP._Page_Views_home_Test_cshtml.Execute() in c:\\inetpub\\wwwroot\\foobarweb_dev\\Views\\Home\\Test.cshtml:line 14",
"RemoteStackTraceString":null,
"RemoteStackIndex":0,
"ExceptionMethod":"8\nGetResponse\nSystem, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089\nSystem.Net.HttpWebRequest\nSystem.Net.WebResponse GetResponse()",
"HResult":-2146233079,
"Source":"System",
"WatsonBuckets":null
}
我已经阅读了编辑以下设置的建议,并且我已经对它们进行了修改,但是无论我使用什么组合,我仍然会遇到上述错误:
http.KeepAlive
http.ProtocolVersion
http.ServicePoint.ConnectionLimit
http.Timeout
如果我在本地计算机上的另一个项目中尝试此操作,连接到该站点的“https”版本会起作用。仅当通过SSL从站点内部连接时,它才是
我被难住了,我甚至不确定这是一个代码问题、服务器问题,甚至可能是代理/防火墙问题。这个问题与SSL有关——可能是某种证书问题。如果它是面向公众的,我会从运行它开始,看看这是否有助于嗅出证书问题 检查主机“”的TLS版本和项目的.NET TLS版本。他们应该是平等的 通过“即时窗口”进行检查:
System.Net.ServicePointManager.SecurityProtocol我遇到了同样的问题,并通过将以下内容添加到我的Powershell脚本中解决了它:
$AllProtocols = [System.Net.SecurityProtocolType]'Ssl3,Tls,Tls11,Tls12'
[System.Net.ServicePointManager]::SecurityProtocol = $AllProtocols
[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy
是的,这是我的第一个猜测,但我不确定如何找出问题所在。我运行了SSL实验室分析器,但没有什么真正的问题:/看起来基本正确。还有两件事要尝试——一件是禁用ssl验证,看看是否可以修复它。另一种方法是尝试在客户机上安装wireshark,看看当请求触发时会发生什么——魔鬼就在细节中的某个地方。这将引导我找到正确的解决方案!我仍然在我的服务器上启用了TLS1.0(根据SSL LAbs Analyzer),但与我交谈的服务器没有。因此,我必须设置ServicePointManager.SecurityProtocol=SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12+我将前两行合并为一行,并在最新更新后解决了我的jira连接问题。非常感谢!