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;
            }
    

      

  • 相关阅读:
    004.Jquery库的用法
    update 死锁问题
    Nginx负载均衡模式
    微信公众号开启服务器配置 JAVA
    mybatis plus + AOP 多数据源自动切换
    mybatis plus 快速上手
    mybits 笔记
    java 异步
    node 垃圾回收机制
    常用正则
  • 原文地址:https://www.cnblogs.com/barrysgy/p/4503853.html
Copyright © 2011-2022 走看看