3C科技 娛樂遊戲 美食旅遊 時尚美妝 親子育兒 生活休閒 金融理財 健康運動 寰宇綜合

Zi 字媒體

2017-07-25T20:27:27+00:00
加入好友
C# HttpWebRequest 無法建立 SSL/TLS 的安全通道   資料來源: https://hk.saowen.com/a/196b225c08d7a25db894d8c0d2176e3039224550172e90de7c8adaefc3e13ff6   在網上查了很多資料,基本是這麼一個思路: 在通過 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); 前面加上如下幾行代碼: System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12; ServicePointManager.ServerCertificatidationCallback = new System.Net.Security.RemoteCertificatidationCallback(CheckValidationResult);//驗證服務器證書回調自動驗證

本文由jashliaoeuwordpress提供 原文連結

寫了 5860316篇文章,獲得 23313次喜歡
精彩推薦