简体   繁体   English

如何在.net上为WebClient设置TimeOut?

[英]How to Set TimeOut for WebClient on .net?

I download some file but I also want to set timeout for webclient. 我下载了一些文件,但我也想为webclient设置超时。 As I see there is no change just We can use overriding WebRequest. 我看到没有变化只是我们可以使用重写WebRequest。 I already did but It doesnt work. 我已经做了但它不起作用。 I mean tht overriding of GetWebRequest method doesnt work.. Here are my codes 我的意思是重写GetWebRequest方法不起作用..这是我的代码

  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 Download 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 do download asynchrony 注意:我想下载异步

From the MSDN Doc: 来自MSDN Doc:

The Timeout property affects only synchronous requests made with the GetResponse method. Timeout属性仅影响使用GetResponse方法发出的同步请求。 To time out asynchronous requests, use the Abort method. 要使异步请求超时,请使用Abort方法。

So if you are going to do an asynchronous request, I think you need to manage a timer of your own, and call .Abort() on the instance after whatever period of time. 因此,如果您要执行异步请求,我认为您需要管理自己的计时器,并在任何时间段后在实例上调用.Abort()。

There is some sample code showing this on this MSDN page for the .Abort() method. 此MSDN页面上有一些示例代码显示.Abort()方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM