How to set TimeOut for WebClient in .net?

I am uploading some file, but I also want to set a timeout for webclient. As I see, there are no changes. We can use an overriding WebRequest. I already did, but it does not work. I mean that overriding the GetWebRequest method does not work. Here are my codes.

public class VideoDownloader : Downloader { /// <summary> /// Initializes a new instance of the <see cref="VideoDownloader"/> class. /// </summary> /// <param name="video">The video to download.</param> /// <param name="savePath">The path to save the video.</param> public VideoDownloader(VideoInfo video, string savePath) : base(video, savePath) { } /// <summary> /// Starts the video download. /// </summary> public override void Execute() { // We need a handle to keep the method synchronously var handle = new ManualResetEvent(false); var client = new WebClient(); client.DownloadFileCompleted += (sender, args) => handle.Set(); client.DownloadProgressChanged += (sender, args) => this.OnProgressChanged(new ProgressEventArgs(args.ProgressPercentage)); this.OnDownloadStarted(EventArgs.Empty); client.DownloadFileAsync(new Uri(this.Video.DownloadUrl), this.SavePath); handle.WaitOne(); handle.Close(); this.OnDownloadFinished(EventArgs.Empty); } protected override WebRequest GetWebRequest(Uri address) { WebRequest w = base.GetWebRequest(address); w.Timeout = 10*1000; // 20 * 60 * 1000; return w; } } 

And the loading class

  public abstract class Downloader: WebClient { /// <summary> /// Initializes a new instance of the <see cref="Downloader"/> class. /// </summary> /// <param name="video">The video to download/convert.</param> /// <param name="savePath">The path to save the video/audio.</param> protected Downloader(VideoInfo video, string savePath) { this.Video = video; this.SavePath = savePath; } protected override WebRequest GetWebRequest(Uri address) { WebRequest w = base.GetWebRequest(address); w.Timeout = 10 * 1000; // 20 * 60 * 1000; return w; } /// <summary> /// Occurs when the download finished. /// </summary> public event EventHandler DownloadFinished; /// <summary> /// Occurs when the download is starts. /// </summary> public event EventHandler DownloadStarted; /// <summary> /// Occurs when the progress has changed. /// </summary> public event EventHandler<ProgressEventArgs> ProgressChanged; /// <summary> /// Gets the path to save the video/audio. /// </summary> public string SavePath { get; private set; } /// <summary> /// Gets the video to download/convert. /// </summary> public VideoInfo Video { get; private set; } /// <summary> /// Starts the work of the <see cref="Downloader"/>. /// </summary> public abstract void Execute(); protected void OnDownloadFinished(EventArgs e) { if (this.DownloadFinished != null) { this.DownloadFinished(this, e); } } protected void OnDownloadStarted(EventArgs e) { if (this.DownloadStarted != null) { this.DownloadStarted(this, e); } } protected void OnProgressChanged(ProgressEventArgs e) { if (this.ProgressChanged != null) { this.ProgressChanged(this, e); } } } 

Where is my mistake? Note. I want to download asynchrony.

+6
source share
1 answer

From the MSDN doc:

The Timeout property only affects synchronous requests made using the GetResponse method. To disable asynchronous requests, use the Abort method.

So, if you are going to execute an asynchronous request, I think you need to control your own timer and call the .Abort () method on the instance after some period of time.

Here is an example of code showing this on this MSDN page for the .Abort() method.

+7
source

Source: https://habr.com/ru/post/924865/


All Articles