间歇性SecureChannelFailure错误

时间:2018-03-01 12:34:28

标签: c# ssl httprequest

在向我主持的https://网站发出简单的GET请求时,我不断收到间歇性的SecureChannelFailure错误。没有任何错误进入服务器日志文件。每100个呼叫的频率小于1个错误,但下面的代码是否会导致这些错误?

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36";
request.ContentType = "application/x-www-form-urlencoded";
request.Accept = "text/html, application/xhtml+xml, application/xml;q=0.9, image/webp, image/gif, */*;q=0.8, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/x-silverlight, */*";
request.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip,deflate,sdch");
request.Proxy = null;
request.AllowAutoRedirect = autoRedirect;
request.Headers.Add("Upgrade-Insecure-Requests", "1");
HttpRequestCachePolicy noCachePolicy = new HttpRequestCachePolicy(HttpRequestCacheLevel.NoCacheNoStore);
request.CachePolicy = noCachePolicy;

HttpWebResponse response = (HttpWebResponse)request.GetResponse()

错误:

SecureChannelFailure

System.Net.WebException: The request was aborted: Could not create SSL/TLS secure channel.

更新: 曾经在.net创建的控制台应用程序中调用此功能。我无法使用在同一台Windows机器上运行的python脚本复制同样的问题。那与微软有什么关系?

1 个答案:

答案 0 :(得分:0)

解决了相同的问题,它也发生了大约1%的时间...

在删除旧的安全协议后,仅保留TLS 1.2,效果更好

尝试:

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;