【问题标题】:How to Set TimeOut for WebClient on .net?如何在.net 上为 WebClient 设置超时?
【发布时间】:2012-09-01 11:52:05
【问题描述】:

我下载了一些文件,但我也想为 webclient 设置超时。正如我所看到的,我们可以使用覆盖 WebRequest 没有任何变化。我已经这样做了,但它不起作用。我的意思是覆盖 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;

    }

}

还有下载类

 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);
        }
    }
}

我的错误在哪里? 注意:我想下载异步

【问题讨论】:

    标签: c# timeout webclient webrequest downloadfileasync


    【解决方案1】:

    来自 MSDN 文档:

    Timeout 属性仅影响使用 GetResponse 方法发出的同步请求。要使异步请求超时,请使用 Abort 方法。

    因此,如果您要执行异步请求,我认为您需要管理自己的计时器,并在任何时间后在实例上调用 .Abort()。

    this MSDN page 上的 .Abort() 方法有一些示例代码。

    【讨论】:

    • 我明白了。。谢谢你的关注。我想我会使用计时器。 :)
    猜你喜欢
    • 1970-01-01
    • 2018-02-24
    • 2020-10-12
    • 2019-11-23
    • 2018-08-06
    • 1970-01-01
    • 1970-01-01
    • 2010-12-19
    相关资源
    最近更新 更多