Recently, I have had to face a problem while consuming a web service from my .NET application and would like to share the problem as well as the solution with others.
The error message I received said:
“The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel”.
As I later found out, this error occurs because the server in question is SSL protected but it is not using a trusted certificate. HTTPS/SSL clients always validate the SSL server certificates to ensure the identity of the server. If the server is SSL protected and is not using a trusted certificate, then the client-side will report error by default.
To resolve this problem you need to add the following code at the beginning before the service is invoked to make the client trust certain or all server certificates by usingServicePointManager.ServerCertificateValidationCallback event handler: