在 HttpClientHandler
当中会有一个 ServerCertificateCustomValidationCallback
事件,该事件用于判定证书验证是否通过。我们可以挂接该事件,然后逻辑编写为直接返回 true
结果,这样就会忽略掉证书异常的情况。
代码如下:
var httpClientHandler = new HttpClientHandler { ServerCertificateCustomValidationCallback = (message, certificate2, arg3, arg4) => true //忽略掉证书异常 }; using (HttpClient client = new HttpClient(httpClientHandler)) { var jsonContent = new StringContent(jsonString, Encoding.UTF8, "application/json"); var result = await client.PostAsync("https://************", jsonContent); if (result.IsSuccessStatusCode) { string str = await result.Content.ReadAsStringAsync(); } }
官方使用文档:
使用 HttpClient 发出 HTTP 请求 - .NET | Microsoft Learn
标签:established,exception,ASP,HTTP,result,var,new,NET,HttpClient From: https://www.cnblogs.com/LinWenQiang/p/17148267.html