Connecting to localhost using HttpClient in C#

When getting error message:

The SSL connection could not be established, see inner exception.

Inner exception:

The remote certificate is invalid according to the validation procedure.

We need to add a client handler that will simply accept all certificates no matter what.

private static async Task<HttpResponseMessage> Request(object request, string url, string headerKey, string secret)
{
using (var httpClientHandler = new HttpClientHandler())
{
// Return `true` to allow certificates that are untrusted/invalid
httpClientHandler.ServerCertificateCustomValidationCallback =
HttpClientHandler.DangerousAcceptAnyServerCertificateValidator;

using (var client = new HttpClient(httpClientHandler))
{
client.DefaultRequestHeaders.Add(headerKey, secret);

var reqJson = JsonConvert.SerializeObject(request);
var content = new StringContent(reqJson, Encoding.UTF8, "application/json");
var response = await client.PostAsync(url, content);

return response;
}
}
}