https网站访问第三方https网站时候报错: The request was aborted:Could not create SSL/TLS secure channel.
https网站访问第三方https网站时候报错:
The request was aborted:Could not create SSL/TLS secure channel.
解决办法:
if(Url.StartsWith("https",StringComparison.OrdinalIgnoreCase))//https请求 { ServicePointManager.Expect100Continue = true; //如果是4.5以上版本可以直接使用 //ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls // | SecurityProtocolType.Tls11 // | SecurityProtocolType.Tls // | SecurityProtocolType.Ssl3; ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | (SecurityProtocolType)768 | (SecurityProtocolType)3072 | SecurityProtocolType.Ssl3; ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(CheckValidationResult); request = WebRequest.Create(Url_Temp) as HttpWebRequest; request.ProtocolVersion = HttpVersion.Version10; } else { request = WebRequest.Create(Url_Temp) as HttpWebRequest; }
回调函数:
private static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { //为了通过证书验证,总是返回true return true; }
然而事情并没有这么简单,按上述方法改好程序后,在有些电脑是能正常工作了,但在有台电脑仍然报错,只不过报错变成了“The requested security protocol is not supported”,中文应该是“不支持请求的安全协议”。搜索得知,需要在电脑上安装.net 4.5或更高版本的框架才行,对,即便程序项目框架只是4.0。
也就是说,如果操作系统是nt5.x(xp/2003),没戏,因为XP最高只能安装到.net 4.0,只能升级系统;而如果程序是基于4.0以下的版本,如2.0、3.5,那安装4.5+也不行,能不能解决和怎么解决我不知道,这篇文章看似相关,但我没实践,读者有需要的话可自行尝试。
相信随着越来越多服务端采用新协议,老迈的nt5.x以后会连上网都成问题,这是一个活生生的因发展而造成老产品被实质淘汰的案例。