Welcome to deBUG.to Community where you can ask questions and receive answers from Microsoft MVPs and other experts in our community.
1 like 0 dislike
11k views
in .Net Framework by 66 69 85

I'm trying to consume a REST API, and this API need an https connection. But i'm stuck on this Error:

  • System.Net.Http.HttpRequestException: 'The SSL connection could not be established, see inner exception.

which, due to the certificate's untrust or something similar, throw the previous exception.

Is there a way to bypass the certificate and get the response?


1 Answer

1 like 0 dislike
by 66 69 85
 
Best answer

How to bypass invalid SSL certificate.

You can bypass the certificate check, and solve this error, by override the certificate check on the callback function on the HttpClientHandler like the below code.

HttpClientHandler clientHandler = new HttpClientHandler();
clientHandler.ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) => { return true; };

using(var httpClient = new HttpClient(clientHandler))
{
 // your code
}
If you don’t ask, the answer is always NO!
...