系統從.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
