0

The timeout is not respected when using downloadFile:

From: https://stackoverflow.com/a/3052637/6771190

public class WebDownload : WebClient
{
  public int Timeout { get; set; }

  public WebDownload(int timeout)
  {
      this.Timeout = timeout;
  }

  protected override WebRequest GetWebRequest(Uri address)
  {
      WebRequest request = base.GetWebRequest(address);
      if (request != null)
      {
          request.Timeout = this.Timeout;
      }
      return request;
  }
}

The code using the above class is:

public new bool TerInternetGet(string url, string OutFile)
{
int timeout = 4000; 
WebDownload client = new WebDownload(timeout);
 try
 {
     client.DownloadFile(url, OutFile);
 }
 catch (Exception ex)
 {
     Debug.WriteLine(ex.ToString());
     return false;
 }
 return true;
}

I noticed that when timeout is set to 40 ms,timeout is respected but when it is about 4000 ms, timeout is not respected.

Ex: This url certainly takes more than 4 secs to complete. But my program is not timing out. http://eoimages.gsfc.nasa.gov/images/imagerecords/74000/74393/world.topo.200407.3x5400x2700.png

Interesting observation: When I have Fiddler open, all timeouts are respected but not when its closed.

Zoe
  • 27,060
  • 21
  • 118
  • 148
ykhandel
  • 29
  • 3
  • 1
    I fail to see the purpose of you class. Why do you expect an exception to be thrown? Have you just tried? https://msdn.microsoft.com/en-us/library/system.net.webrequest.timeout(v=vs.110).aspx – Jocke Aug 31 '16 at 17:55
  • @Jocke When I set the timeout = 40 ms, I get this exception: _italic_System.Net.WebException: The operation has timed out. From the msdn: "The Timeout property indicates the length of time, in milliseconds, until the request times out and throws a WebException." – ykhandel Aug 31 '16 at 18:07
  • You're not even using the timeout. Do you have code that you didn't include? – Brandon Aug 31 '16 at 18:27
  • @Brandon, when the timeout happens, an exception will be caught and the function returns false. – ykhandel Aug 31 '16 at 18:40

0 Answers0