I use this code in my .Net Core application to ignore SSL connection exception:
ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyErrors) => true;
ServicePointManager.MaxServicePointIdleTime = 0;
Problem is it doesn't work on first or second run and it works fine after that until application gets idle again. If application gets idle, first or second run fails again.
Here is full code:
ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyErrors) => true;
ServicePointManager.MaxServicePointIdleTime = 0;
using (var client = new WebClient())
{
try
{
client.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
string response = client.UploadString(URL, "POST", $"username={Username}&password={Password}");
}
catch (WebException ex)
{
if (ex.Response != null)
ErrorMessage = new StreamReader(ex.Response.GetResponseStream()).ReadToEnd();
ErrorMessage = ex.Message;
}
}
Error message:
The SSL connection could not be established, see inner exception.