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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM