在通過
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url); req.Method = "GET"; HttpWebResponse sp = (HttpWebResponse)req.GetResponse();作處理時(shí),有些輸入有些URL會在HttpWebResponse sp = (HttpWebResponse)req.GetResponse();的時(shí)候拋出一個(gè)“基礎(chǔ)連接已經(jīng)關(guān)閉: 未能為 SSL/TLS 安全通道建立信任關(guān)系”的異常。 最簡單的辦法是: 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);//驗(yàn)證服務(wù)器證書回調(diào)自動驗(yàn)證新聞熱點(diǎn)
疑難解答