mmz
mmz

Reputation: 49

"The underlying connection was closed: An unexpected error occurred on a send." Error in WebClient.OpenRead(string link)

I am facing the Error "The underlying connection was closed: An unexpected error occurred on a send." in getting return data of google API.
It was fine locally but I had published and upload to server, I faced that Error. Help me please.. Best Regard...

 WebClient webClient = new WebClient();
 Stream stream = webClient.OpenRead("https://www.googleapis.com/oauth2/v1/userinfo?access_token=[Access_Token]);
 string b;

/*I have not used any JSON parser because I do not want to use any extra dll/3rd party dll*/
 using (StreamReader br = new StreamReader(stream))
      {
         b = br.ReadToEnd();
      }

Upvotes: 2

Views: 12877

Answers (2)

AhmadReza Payan
AhmadReza Payan

Reputation: 2271

This problem occurs when the client computer cannot send an HTTP request. The client computer cannot send the HTTP request because the connection has been closed or is unavailable. This problem may occur when the client computer is sending lots of data.

Visit https://support.microsoft.com/en-us/kb/915599

See resolutions A, D, E, F, and O may help you.

Update 1:

Unfortunately, The above link is broken. Read the following please.

To accommodate TLS1.2 in .Net Framework 4.0, try this:

ServicePointManager.SecurityProtocol = DirectCast(3072, SecurityProtocolType)

If the TLS 1.2 doesn't work for you, try the following:

System.Net.ServicePointManager.SecurityProtocol =
    SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;

Here is some information about TLS and .NET support:

  1. .NET 4.6 and above. You don’t need to do any additional work to support TLS 1.2, it’s supported by default.

  2. .NET 4.5. TLS 1.2 is supported, but it’s not a default protocol. You need to opt-in to use it. The following code will make TLS 1.2 default, make sure to execute it before making a connection to secured resource: ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12

  3. .NET 4.0. TLS 1.2 is not supported, but if you have .NET 4.5 (or above) installed on the system then you still can opt in for TLS 1.2 even if your application framework doesn’t support it. The only problem is that SecurityProtocolType in .NET 4.0 doesn’t have an entry for TLS1.2, so we’d have to use a numerical representation of this enum value: ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;

  4. .NET 3.5 or below. TLS 1.2 is not supported (*) and there is no workaround. Upgrade your application to more recent version of the framework.

For more information about TLS and .NET Support, visit the following link:

TLS 1.2 and .NET Support: How to Avoid Connection Errors

Upvotes: 1

Fiach Reid
Fiach Reid

Reputation: 7059

Your connection is connecting to a HTTPS endpoint, it may be possible that it requires TLS 1.2, which may not be the default TLS version of your client; this is a fix for older versions of .NET:

ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;

Upvotes: 10

Related Questions