Last active
August 28, 2019 14:30
-
-
Save relyky/1b4e562bd36736f4506c to your computer and use it in GitHub Desktop.
當要求https連線出現:基礎連接已關閉: 無法為 SSL/TLS 安全通道建立信任關係。
This file contains hidden or 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:http://blog.darkthread.net/post-2010-05-06-webclient-ssl-dismatch.aspx | |
//參考來源2:http://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("https://192.168.1.1/Page.aspx"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment