HttpClient SSL connection could not be established error

系统从.net framework 升级到dotnet core2.1 

原先工作正常的httpclient,会报SSL connection could not be established error 错误 

 

在.net framework中通过ServicePointManager,当证书出错时,可以跳过证书验证。

ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyErrors) => true

升级到dotnet core 2.1后,发现以上配置无效。

  原因是ServicePointManager只对HttpWebRequest有效,.net framework中httpclient是基于HttpWebRequest实现的,所以Httpclient不会报错

在dotnetcore2.1中,Httpclient想要实现相同的功能,需要在HttpClient中设置。

 var handler = new HttpClientHandler
 {
     ServerCertificateCustomValidationCallback = delegate { return true; }
 };

github上有人提过这个问题,可以参见 https://github.com/dotnet/corefx/issues/29452

posted @ 2019-07-04 18:09  chenzhenfj  阅读(2105)  评论(1编辑  收藏  举报