HttpClient请求HTTP时证书错误
错误信息
1 | System.Net.Http.HttpRequestException: 发送请求时出错。 ---> System.Net.WebException: 基础连接已经关闭: 未能为 SSL/TLS 安全通道建立信任关系。 ---> System.Security.Authentication.AuthenticationException: 根据验证过程,远程证书无效。 |
解决方法
设置HttpClient的证书验证方式
引入
System.Net.Http.WebRequest
设置HttpClient的
Handler
,使其证书验证永远返回True1
HttpClient Client = new HttpClient(new WebRequestHandler() { ServerCertificateValidationCallback = (ser, cer, chn, err) => { return true; }, });
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 AlMirai!