HTTPS完美解决“请求被中止: 未能创建 SSL/TLS 安全通道”

HttpWebRequest 请求第三方https接口,一直提示未能创建 SSL/TLS 安全通道! 证书验证创建委托一直返回true 也不行。

public bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors) { return true; }


            ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(CheckValidationResult);

设置ServicePointManager.SecurityProtocol也不行,试过Ssl3,Tls,Tls11,Tls12

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

 

导入证书依然不行 

request.ClientCertificates.Add(new X509Certificate(AppDomain.CurrentDomain.BaseDirectory + "sinho1.cer"));

测试完整代码

HttpWebRequest request = null;
            request = (HttpWebRequest)HttpWebRequest.Create(this._UriString);
            request.ContentType = "application/x-www-form-urlencoded";
            request.Accept = "text/html, application/xhtml+xml, */*";
            request.UserAgent = "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36";
            request.AllowAutoRedirect = this._AllowAutoRedirect;
            request.CookieContainer = new CookieContainer();
            request.Referer = this._Referer;
            request.Timeout = 120000;
            request.ReadWriteTimeout = 120000;
            request.ServicePoint.Expect100Continue = true;
            request.ServicePoint.ConnectionLimit = 1024;
            request.KeepAlive = true;
            //request.ClientCertificates.Add(new X509Certificate(AppDomain.CurrentDomain.BaseDirectory + "sinho1.cer"));
            //request.ProtocolVersion = HttpVersion.Version10;
            if (this.Header != null && this.Header.Count > 0)
            {
                foreach (var item in Header)
                {
                    SetHeaderValue(request.Headers, item.key, item.value);
                }
            }
            if (this._Accept != null)
                request.Accept = this._Accept;

            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;   //协议按需选择,不行就都试一遍
            ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(CheckValidationResult);

 

最终找到一个最简单的解决办法:将.net 版本设置.NET Framework 4.6及以上,完美解决!

你可能感兴趣的:(HTTPS完美解决“请求被中止: 未能创建 SSL/TLS 安全通道”)