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

如提

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

參考來源

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

https://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();
                }
            }

就可以解決了