悉野小楼

导航

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

  

posted on 2015-05-14 17:20  悉野  阅读(9396)  评论(0编辑  收藏  举报