由于以下错误消息,我们无法使用WebRequest
连接到HTTPS服务器:
The request was aborted: Could not create SSL/TLS secure channel.
我们知道服务器没有有效的HTTPS证书和使用的路径,但是为了绕过这个问题,我们使用了下面的代码,这些代码取自另一篇StackOverflow文章:
private void Somewhere() {
ServicePointManager.ServerCertificateValidationCallback += new RemoteCertificateValidationCallback(AlwaysGoodCertificate);
}
private static bool AlwaysGoodCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors policyErrors) {
return true;
}
问题是服务器从未验证过证书,并失败并出现上述错误。有谁知道我该怎么做吗?
值得一提的是,我和一位同事几周前进行了测试,使用类似于我上面写的东西,它工作得很好。我们发现的唯一“主要区别”是我用的是Windows7,而他用的是Windows XP。这会改变什么吗?
发布于 2018-02-22 22:46:22
在.NET 4.5中,这个问题的解决方案是
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
如果您没有.NET 4.5,那么可以使用
ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;
发布于 2018-06-22 05:39:39
确保在创建HttpWebRequest之前进行了ServicePointManager设置,否则它将无法工作。
作品:
ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls
| SecurityProtocolType.Tls11
| SecurityProtocolType.Tls12
| SecurityProtocolType.Ssl3;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://google.com/api/")
失败:
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://google.com/api/")
ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls
| SecurityProtocolType.Tls11
| SecurityProtocolType.Tls12
| SecurityProtocolType.Ssl3;
发布于 2014-10-16 01:42:49
我在尝试访问https://ct.mob0.com/Styles/Fun.png时遇到了这个问题,这是一个由CloudFlare在其CDN上分发的图像,它支持像SPDY和奇怪的重定向SSL证书这样的疯狂东西。
我没有像在Simons answer中那样指定Ssl3,而是能够通过如下方式进入Tls12来修复它:
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
new WebClient().DownloadData("https://ct.mob0.com/Styles/Fun.png");
https://stackoverflow.com/questions/2859790
复制相似问题