在通過
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url); req.Method = "GET"; HttpWebResponse sp = (HttpWebResponse)req.GetResponse();作處理時,有些輸入有些URL會在HttpWebResponse sp = (HttpWebResponse)req.GetResponse();的時候拋出一個“基礎連接已經關閉: 未能為 SSL/TLS 安全通道建立信任關系”的異常。 最簡單的辦法是: 1,先加入命名空間:
using System.Net.Security;using System.Security.Authentication;using System.Security.Cryptography.X509Certificates;2,再重載CheckValidationResult方法,返回true
public bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors) { // 總是接受 return true; }3,然后在
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url);前面加上如下一行代碼:
ServicePointManager.ServerCertificatidationCallback = new System.Net.Security.RemoteCertificatidationCallback(CheckValidationResult);//驗證服務器證書回調自動驗證新聞熱點
疑難解答