Pinki
Pinki

Reputation: 71

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

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;
    }

Upvotes: 1

Views: 9267

Answers (1)

Manoj Choudhari
Manoj Choudhari

Reputation: 5634

Somehow your code is trying to use TLS but the server to which you are sending request does not support it.

You can try setting SSL3 explicitly

Change:

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

To:

ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;

Upvotes: 1

Related Questions