zoukankan      html  css  js  c++  java
  • C# 访问https 未能创建 SSL/TLS 安全通道

    C# 访问https请求被中止: 未能创建 SSL/TLS 安全通道(Could not create SSL/TLS secure channel)

    一般GetResponse可以直接访问https,如果不行添加回调:

    ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(CheckValidationResult);
    还不行, 添加: ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;

       
            public string GetHttpWebResponse(string url, string postData, string code = "utf-8")
            {
                ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;
                ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(CheckValidationResult);
                HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
                HttpWebResponse response = null;
                try
                {
                    response = (HttpWebResponse)request.GetResponse();
                    using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(code)))
                    {
                        return reader.ReadToEnd();
                    }
                }
                catch (WebException e)
                {
                    return e.Message;
                }
                finally
                {
                    if (response != null) response.Close();
                }
            }
    
            public static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
            {
                return true;
            }
    

      

  • 相关阅读:
    第01组 Beta冲刺(2/5)
    第01组 beta冲刺(1/5)
    软工实践个人总结
    第01组 每周小结(3/3)
    第01组 每周小结(2/3)
    第01组 每周小结 (1/3)
    第01组_Beta冲刺总结
    第01组 Beta冲刺(5-5)
    第01组 Beta冲刺(4-5)
    第01组 Beta冲刺(3-5)
  • 原文地址:https://www.cnblogs.com/barrysgy/p/4503853.html
Copyright © 2011-2022 走看看