码迷,mamicode.com
首页 > Windows程序 > 详细

C# 访问https 未能创建 SSL/TLS 安全通道

时间:2015-05-14 18:03:34      阅读:2317      评论:0      收藏:0      [点我收藏+]

标签:

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

  

C# 访问https 未能创建 SSL/TLS 安全通道

标签:

原文地址:http://www.cnblogs.com/barrysgy/p/4503853.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!