https 请求被中止: 未能创建 SSL/TLS 安全通道

WebClient 调用本地P12证书,之前一直报错,后面找了N多资料,终于在stackOverFlow找到了蛛丝马迹,下面一定要用X509Certificate2 这个证书类,记得带了"2",

如果不带,本地电脑或IE、程序 两者都必须要注册证书!!!

这事情折腾了我一整天!


internal class MyWebClient : WebClient

    {
        protected override WebRequest GetWebRequest(Uri address)
        {
            HttpWebRequest request = (HttpWebRequest)base.GetWebRequest(address);
            if (address.AbsoluteUri.ToLower().StartsWith("https:"))
            {
                System.Net.ServicePointManager.ServerCertificateValidationCallback = delegate (Object obj, X509Certificate X509certificate, X509Chain chain, System.Net.Security.SslPolicyErrors errors)
                {
                    return true;
                };

                X509Certificate2 certificate = new X509Certificate2(Properties.Resources.client, Properties.Resources.httpsKey8843);
                request.ClientCertificates.Add(certificate);
            }

            request.Timeout = 600000;//设置超时为10分钟
            request.ReadWriteTimeout = 600000;
            return request;
        }
    }

你可能感兴趣的:(https 请求被中止: 未能创建 SSL/TLS 安全通道)