Timeout not respected when using downloadFile (Overriding GetWebRequest to set timeout )

时间:2016-08-31 17:42:14

标签: c# download httpwebrequest webclient downloadfile

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.

0 个答案:

没有答案