C# webclient - Received an unexpected EOF or 0 bytes from the transport stream

时间:2018-12-27 12:59:41

标签: c# .net webclient

I am facing this error sometimes while I am trying to connect to the server using WebClient.

However I have gone through the previous questions and answers such as Getting EOF exception over https call about this. I have already tried their solutions but that didn't help me.

As per previous questions, I've also updated .net framework from 4.5 to 4.6.1 but still facing the same.

My code is like below

    private static string GetWebContent(string url)
    {
        string response = null;
        try
        {
            ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls | SecurityProtocolType.Ssl3;                
            using (var request = new GZipWebClient())
            {
                response = request.DownloadString(url);
                request.Dispose();
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
        return response;
    }

1 个答案:

答案 0 :(得分:0)

您的代码以某种方式尝试使用TLS,但是您向其发送请求的服务器不支持它。

您可以尝试显式设置SSL3

更改:

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls | SecurityProtocolType.Ssl3;

收件人:

ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;