Webclient "The operation has timed out" and override webclient with customized timeout

 1  public class WebDownload : WebClient
 2     {
 3         /// <summary>
 4         /// Time in milliseconds
 5         /// </summary>
 6         public int Timeout { get; set; }
 7 
 8         public WebDownload() : this(600000) { }
 9 
10         public WebDownload(int timeout)
11         {
12             this.Timeout = timeout;
13         }
14 
15         protected override WebRequest GetWebRequest(Uri address)
16         {
17             var request = base.GetWebRequest(address);
18             if (request != null)
19             {
20                 request.Timeout = this.Timeout;
21             }
22             return request;
23         }
24     }
25 
26 
27 
28 
29 static void WebDownloadDemo()
30         {
31             string url = "https://tfetimes.com/wp-content/uploads/2015/09/c-gui-programming-with-qt-4-2ndedition.pdf";
32             WebDownload wd = new WebDownload();
33             byte[] data = wd.DownloadData(url);
34             File.WriteAllBytes("aaaa.pdf", data);
35         }

 

posted @ 2021-04-07 15:53  FredGrit  阅读(105)  评论(0编辑  收藏  举报