问题描述
我下载了一些文件,但我也想设置超时的WebClient。当我看到没有变化只是我们可以用压倒一切的WebRequest。我已经做了,但它不工作。我的意思是GetWebRequest方法不工作的首要THT。下面是我的代码
I download some file but I also want to set timeout for webclient. As I see there is no change just We can use overriding WebRequest. I already did but It doesnt work. I mean tht overriding of GetWebRequest method doesnt 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;
}
}
而下载类
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
推荐答案
从MSDN文档:
超时属性只影响与GetResponse的方法进行同步请求。超时异步请求,使用Abort方法。
所以,如果你要做一个异步请求,我认为你需要管理自己的计时器,和任何时间段后的实例调用.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()
方法。
这篇关于如何设置超时.NET的Web客户端?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!