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.
Solution:
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!!!
Great. Thank you for sharing
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¤cy=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;
not work
Hi Rob,
Can you tell me the scenario that you are in & the issue that you are facing?
Thanks for posting this code. Works a treat in my WinForms .Net app connecting to HTTPS web sites.