我在 C# 中有一个下载方法。该方法向要下载的 URL 发送请求。但它给出了一些 URL 地址的错误。我遇到的最后一个 URL 是一个 exe 文件 link .
我的方法:
void DownloadProcedure()
{
#region Request-Response
req = WebRequest.Create(url) as HttpWebRequest;
req.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)";
req.AllowAutoRedirect = true;
req.MaximumAutomaticRedirections = 5;
req.ServicePoint.ConnectionLimit += 2;
req.ServicePoint.Expect100Continue = true;
req.ProtocolVersion = HttpVersion.Version10;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls
| SecurityProtocolType.Tls11
| SecurityProtocolType.Tls12
| SecurityProtocolType.Ssl3;
// allows for validation of SSL conversations
ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
if (rangeAllowed)
req.AddRange(from, to);
resp = req.GetResponse() as HttpWebResponse;
#endregion
//...bla bla...
}
在 GetResponse()
行的代码中,它抛出 Could not create SSL/TSL secure channel。但它并不总是扔掉它。有时文件下载成功,有时会抛出此异常。我不知道通过它的正确方法是什么。
那么我该如何解决这个问题呢?A
最佳答案
如果您在使用不同的网址时遇到此问题: 您正在强制使用 Http 1.0。目前,Http 1.0、1.1 和 2 可用。愿你不应该用 Http 1.0 来限制你自己。 此外,一些服务器仍在使用旧的 SSL 版本,因此您可能想添加对它们的支持...
关于c# - 发送 HttpWebRequest 时出现 SSL/TLS 安全通道异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40497237/