从传输流接收到意外的EOF或0个字节。 ServicePointManager的设置无法修复

时间:2019-05-29 21:20:33

标签: c# .net web

当我尝试使用WebClient下载字符串时,出现该异常。 我尝试了很多情况来设置ServicePointManager:

ServicePointManager.Expect100Continue: true and false
ServicePointManager.SecurityProtocol: SecurityProtocolType.Ssl3 

SecurityProtocolType.Tls12 

(SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls12) 

和所有Tls 以及所有协议。 每个组合都被使用。不起作用。
但是,如果我使用curl命令-我会得到很好的json。

curl -X GET https://myurl/2019-05-29

浏览器的行为相同
系统: Windows Server 2012 R2
.NET:框架4.6.1

呼叫堆栈
Failed to get fixtures from URI=https://myurl/2019-05-29. System.Net.WebException: An exception occurred during a WebClient request. --->System.IO.IOException: Received an unexpected EOF or 0 bytes from the transport stream. at System.Net.ConnectStream.Read(Byte[] buffer, Int32 offset, Int32 size) at System.Net.WebClient.DownloadBitsState.RetrieveBytes(Int32& bytesRetrieved) at System.Net.WebClient.DownloadBits(WebRequest request, Stream writeStream, CompletionDelegate completionDelegate, AsyncOperation asyncOp) at System.Net.WebClient.DownloadDataInternal(Uri address, WebRequest& request) --- End of inner exception stack trace --- at System.Net.WebClient.DownloadDataInternal(Uri address, WebRequest& request) at System.Net.WebClient.DownloadString(Uri address) at System.Net.WebClient.DownloadString(String address)

一种情况下的代码:

ServicePointManager.ServerCertificateValidationCallback +=
              (sender, cert, chain, sslPolicyErrors) => true;
ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls |
                                       SecurityProtocolType.Tls11 | 
                                       SecurityProtocolType.Tls12 |
                                       SecurityProtocolType.Ssl3;

using (var wc = new WebClient())
{
    try
    {
       var json = wc.DownloadString(uri);
       return json;
    }
    catch (Exception e)
    {
        return null;
    }
}

禁用代理并遇到另一个异常,curl运行良好:
Failed to get fixtures from URI=https://myurl/2019-05-29. System.Net.WebException: An exception occurred during a WebClient request. ---> System.IO.IOException: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host. ---> System.Net.Sockets.SocketException: An existing connection was forcibly closed by the remote host 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.ConnectStream.Read(Byte[] buffer, Int32 offset, Int32 size) at System.Net.WebClient.DownloadBitsState.RetrieveBytes(Int32& bytesRetrieved) at System.Net.WebClient.DownloadBits(WebRequest request, Stream writeStream, CompletionDelegate completionDelegate, AsyncOperation asyncOp) at System.Net.WebClient.DownloadDataInternal(Uri address, WebRequest& request) --- End of inner exception stack trace --- at System.Net.WebClient.DownloadDataInternal(Uri address, WebRequest& request) at System.Net.WebClient.DownloadString(Uri address) at System.Net.WebClient.DownloadString(String address))

0 个答案:

没有答案