Vb NET 基礎連接已關閉: 無法為 SSL TLS 安全通道建立信任關係

This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters

//參考來源1://blog.darkthread.net/post-2010-05-06-webclient-ssl-dismatch.aspx
//參考來源2://awei791129.pixnet.net/blog/post/24463385-%5Bc%23%5D-%E7%95%B6%E8%A6%81%E6%B1%82https%E9%80%A3%E7%B7%9A%E5%87%BA%E7%8F%BE%EF%BC%8C%E5%9F%BA%E7%A4%8E%E9%80%A3%E6%8E%A5%E5%B7%B2%E9%97%9C%E9%96%89%3A-%E7%84%A1
關鍵參教
xxx.ServerCertificateValidationCallback = delegate { return true; };
// 狀況一:改連線設定-範例
System.Net.ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
// 狀況一:使用HttpWebRequest的解法-範例
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
// Set some reasonable limits on resources used by this request
request.MaximumAutomaticRedirections = 4;
request.MaximumResponseHeadersLength = 4;
request.Timeout = (int)numTimeOut.Value; // milliseconds
// Set credentials to use for this request.
request.Credentials = CredentialCache.DefaultCredentials;
request.AuthenticationLevel = (AuthenticationLevel)cbxAuthLevel.SelectedValue; // AuthenticationLevel.None;
// 排除此Exception觸發:基礎連接已關閉: 無法為 SSL/TLS 安全通道建立信任關係。
// The underlying connection was closed: Could not establish trust relationship for SSL/TLS secure channel
request.ServerCertificateValidationCallback = delegate { return true; };
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
// 狀況三:使用WebClient的解法-範例
WebClient wc = new WebClient();
ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
string s = wc.DownloadString("//192.168.1.1/Page.aspx");

公司將一些網站改走SSL後發現有些POST取資料功能失效了
出現基礎連接已關閉: 無法為 SSL/TLS 安全通道建立信任關係
解法辦法

ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyErrors) => true;<=加入 ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;<=加入

參考來源
[faq]解決C#呼叫有ssl憑證問題的網站出現遠端憑證是無效的錯誤問題
C# HttpReques 基礎連接已關閉: 無法為 SSL/TLS 安全通道建立信任關係 強制跳過 TLS驗證
C# 連線 HTTPS 網站發生驗證失敗導致基礎連接已關閉

2019-07-07

C# HttpReques 基礎連接已關閉: 無法為 SSL/TLS 安全通道建立信任關係 強制跳過 TLS驗證

  • 7894
  • 0
  • C#
  • 2019-10-16

如提

C#  HttpReques 基礎連接已關閉: 無法為 SSL/TLS 安全通道建立信任關係    強制跳過 TLS驗證

參考來源

//www.coco-in.net/thread-150342-1-1.html

//blog.alantsai.net/posts/2017/12/csharp-ssl-remote-validation-error

可能會遇到這個畫面

實際寫法

    ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyErrors) => true;
    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

   HttpWebRequest request = HttpWebRequest.Create(targetUrl) as HttpWebRequest;
                request.Method = "GET";
            request.ContentType = "application/x-www-form-urlencoded";

            string jsondata = "";
            // 取得回應資料
            using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
            {
                using (StreamReader sr = new StreamReader(response.GetResponseStream()))
                {
                    jsondata = sr.ReadToEnd();
                }
            }

就可以解決了

以上文章僅用紀錄資料使用.....

Toplist

最新的帖子

標籤