WebRequest.GetResponse() throwing SSL secure channel error

Problem Statement:

WebRequest.GetResponse() is one of the most common ways we adopt to perform a HTTPWebRequest from our server side. This case was a specific one, although the error appeared to be more generic in nature. In fact it happened only in SharePoint and not in my .Net application.

The error observed in the GetResponse() method was:

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

One of the root cause of this problem is due to the certificate issue. And to be more precise the certificate validation has failed.


The solution to this problem, more or less i would call it a decent workaround is to use the ServerCertificateCallbackValidation method from the HttpWebRequest class and forcefully return true.

Create a static method for validating the certificate

private static bool ValidateFbCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors policyErrors)
return true;

Just before u make your HttpWebRequest try using this.

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

HttpWebRequest …..;

This should skip any certificate errors.

Happy Coding!!!

  1. Great. Thank you for sharing


  2. Not working . see my code

    ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(ValidateFbCertificate);
    WebRequest webRequest = WebRequest.Create(@”https://secure.profitorius.com/cgi-bin/api.pl?username=xxxx&password=xxxxx &type=sale&totamt=100.00&currency=USD&ccn=4111111111111111&ccexp=1218&cvv=123&orderid=100001&orderdesc=description&pon=1&shipping=10.00&tax=15.00&bfn=Fred&bln=Smith&bcountry=CA&bstate=ON&baddress1=address1&baddress2=address2&bcity=Toronto&bzip=M5S2T2&bphone=4161234567&bfax=4161234568&bemail=fred@hotmail.com&sasaba=1″);
    webRequest.ContentType = “text/html”;
    webRequest.Method = “POST”;
    string body = “…”;
    byte[] bytes = Encoding.ASCII.GetBytes(body);
    webRequest.ContentLength = bytes.Length;

                  ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(ValidateFbCertificate);
                  var os = webRequest.GetRequestStream();
                  os.Write(bytes, 0, bytes.Length);
                  webRequest.Timeout = 0; //setting the timeout to 0 causes the request to fail
                  WebResponse webResponse = webRequest.GetResponse(); //Exception thrown here ..


    1. Hi Rob,

      Can you tell me the scenario that you are in & the issue that you are facing?


  3. Thanks for posting this code. Works a treat in my WinForms .Net app connecting to HTTPS web sites.


Feel free to leave a reply here...

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: