C# 使用webclient.DownloadFileAsync下载的文件有0 KB
我正在尝试使用WebClient下载zend framework(从)C# 使用webclient.DownloadFileAsync下载的文件有0 KB,c#,.net,download,webclient,downloadfileasync,C#,.net,Download,Webclient,Downloadfileasync,我正在尝试使用WebClient下载zend framework(从) string url = "http://framework.zend.com/releases/ZendFramework-1.11.11/ZendFramework-1.11.11.zip"; WebClient downloader= new WebClient(); downloader.DownloadFileAsync(new Uri(url), "C:\\temp.zip"); 文件已创建,但为空。我使用fi
string url = "http://framework.zend.com/releases/ZendFramework-1.11.11/ZendFramework-1.11.11.zip";
WebClient downloader= new WebClient();
downloader.DownloadFileAsync(new Uri(url), "C:\\temp.zip");
文件已创建,但为空。我使用fiddler检查了响应,得到了HTTP 200,内容长度正确,但“connection:closed”和fiddler在“body”列中显示“-1”
我曾尝试在标题中添加用户代理(从谷歌chrome请求复制)和“连接:保持活动”,但这些都没有帮助。我也很确定,我的程序以前使用相同的URL下载过这个文件一两次。WebClient触发的事件中没有错误
有什么想法吗?尝试处理
DownloadProgressChanged
和DownloadFileCompleted
事件
private void button1_Click(object sender, EventArgs e)
{
string url = "http://framework.zend.com/releases/ZendFramework-1.11.11/ZendFramework-1.11.11.zip";
WebClient downloader = new WebClient();
downloader.DownloadFileCompleted += new AsyncCompletedEventHandler(downloader_DownloadFileCompleted);
downloader.DownloadProgressChanged += new DownloadProgressChangedEventHandler(downloader_DownloadProgressChanged);
downloader.DownloadFileAsync(new Uri(url), "C:\\temp.zip");
}
void downloader_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
label1.Text = e.BytesReceived + " " + e.ProgressPercentage;
}
void downloader_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
{
if (e.Error != null)
MessageBox.Show(e.Error.Message);
else
MessageBox.Show("Completed!!!");
}
尝试处理
DownloadProgressChanged
和DownloadFileCompleted
事件
private void button1_Click(object sender, EventArgs e)
{
string url = "http://framework.zend.com/releases/ZendFramework-1.11.11/ZendFramework-1.11.11.zip";
WebClient downloader = new WebClient();
downloader.DownloadFileCompleted += new AsyncCompletedEventHandler(downloader_DownloadFileCompleted);
downloader.DownloadProgressChanged += new DownloadProgressChangedEventHandler(downloader_DownloadProgressChanged);
downloader.DownloadFileAsync(new Uri(url), "C:\\temp.zip");
}
void downloader_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
label1.Text = e.BytesReceived + " " + e.ProgressPercentage;
}
void downloader_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
{
if (e.Error != null)
MessageBox.Show(e.Error.Message);
else
MessageBox.Show("Completed!!!");
}
我猜:也许你可以试着把WebClient实例放在某个不会被垃圾收集的地方。当触发DownloadFileCompleted事件时,您只需清除对WebClient实例的引用,并让GC稍后回收内存(不要忘记调用Dispose方法)。我猜:也许您可以尝试将WebClient实例保留在某个不会被垃圾收集的位置。当启动DownloadFileCompleted事件时,您只需清除对WebClient实例的引用,并让GC稍后回收内存(不要忘记调用Dispose方法)。好的,我终于找到了答案!在下载该文件之前,我通过发送HttpWebRequest检查其大小。问题是,我没有关闭()响应
谢谢你的回答,它们是很好的线索。好的,我终于找到了答案!在下载该文件之前,我通过发送HttpWebRequest检查其大小。问题是,我没有关闭()响应
感谢您的回答,这些都是很好的线索。如果您在Windows“C:\temp.zip”中启用了UAC,则下一行将无法保存文件,因为如果没有提升的权限,您不允许在用户目录之外进行写操作:
downloader.DownloadFileAsync(new Uri(url), "C:\\temp.zip");
如果在Windows“C:\temp.zip”中启用了UAC,则以下行中的文件将无法保存,因为如果没有提升的权限,您不允许在用户目录之外进行写入:
downloader.DownloadFileAsync(new Uri(url), "C:\\temp.zip");
谢谢你用同样的方法解决了我的问题。只是在using语句中包装了web响应,它解决了我的问题。我在下载文件之前检查了文件名,并使用httpwebrequest获取contentDisposition头。谢谢,用同样的方法修复了我的问题。只是在using语句中包装了web响应,它解决了我的问题。我在下载之前检查了文件名,并使用httpwebrequest获取contentDisposition头。