當我們有時用代碼編寫post請求url遠程地址會報“基礎連接已經關閉: 未能為 SSL/TLS 安全通道建立信任關系。 ---> System.Security.Authentication.AuthenticationException: 根據驗證過程,遠程證書無效。”這個異常,是因為遠程url使用的域名 沒有購買證書,所以用以下方式來解決:
ps:在create url之前 設定“獲取或設置用於驗證服務器證書的回調”永遠為true 即可,具體如下
post請求一定需要:System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);
在這句代碼前加上: ServicePointManager.ServerCertificateValidationCallback += RemoteCertificateValidate;即可
RemoteCertificateValidate事件代碼如下:
private static bool RemoteCertificateValidate(object sender, X509Certificate cert,X509Chain chain, SslPolicyErrors error)
{
//為了通過證書驗證,總是返回true
return true;
}
搞定