Wednesday, August 29

The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel.

I was getting an error message "The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel." when trying to connect to an HTTPS site using an HttpWebRequest.

As the site I was connecting to was a test site from a 3rd party known to me, but not set up correctly at their end, to bypass this check I added the following line before calling GetRequestStream on my HttpWebRequest.


ServicePointManager.ServerCertificateValidationCallback
+= new
System.Net.Security.RemoteCertificateValidationCallback
(CustomValidation);


and added the CustomValidation function as follows:


private static bool CustomValidation(object sender,
X509Certificate cert, X509Chain chain,
System.Net.Security.SslPolicyErrors error)
{
return true;
}

2 comments:

Anonymous said...

Easiest solution I found to this problem off google. Thanks mate!

Chirag said...

Good one keep it up :)