好的,对于我们的网站,我要说的是" https://www.foo-bar.com",并且是.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);
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从站点内进行连接时, 仅 。
我很难过,我甚至不确定这是代码问题,服务器问题,还是代理/防火墙问题。
答案 0 :(得分:2)
这个问题与SSL有关 - 可能是某种证书问题。如果它是面向公众的,我将首先运行SSL Labs Analyizer来查看是否有助于嗅出证书问题。
答案 1 :(得分:2)
检查主机的TLS版本" https://www.foo-bar.com"和项目的.NET TLS版本。它们应该是平等的。
检查"立即窗口":
System.Net.ServicePointManager.SecurityProtocol
答案 2 :(得分:1)
我遇到了同样的问题,并通过在Powershell脚本中添加以下内容解决了该问题:
$AllProtocols = [System.Net.SecurityProtocolType]'Ssl3,Tls,Tls11,Tls12'
[System.Net.ServicePointManager]::SecurityProtocol = $AllProtocols
[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy