I have a download method in C#. The method sends request to the URL to be downloaded. But it gives error for some URL address. The last URL that has problem I faced is an exe file link.
My method:
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...
}
In this code at GetResponse()
line it throws Could not create SSL/TSL secure channel. But it doesn't throw it always. Sometimes the file is downloaded successfully and sometimes it throws this exception. I don't know what is the right way to get through it.
So how can I fix this problem?A