WebRequest and SSL and forcibly closed by the remote host

Ever run into this Exception when trying to call a service via https?

Here is some boiler plate code to call the the service via a webrequest


var request = (HttpWebRequest)HttpWebRequest.Create(uri);
if (Settings.Credentials != null)
request.Credentials = Settings.Credentials;
var response = (HttpWebResponse)request.GetResponse();
if (response.StatusCode != HttpStatusCode.OK)
throw new WebException("Web Request to '" + uri + "' failed with code: " + response.StatusCode);

This looks fine and dandy but when you run the code you get the exception:

“Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host”.

What the heck? To make matters worse you can navigate to the URL in a browser,
enter the credentials and you get the response back.

Well one thing to try is to explicitly set the protocol of the request prior to GetResponse.


System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;

I was running into this issue and by adding this line all my issues went away.

Leave a Reply