下载的文件使用webclient.DownloadFileAsync有0KB [英] Downloaded file using webclient.DownloadFileAsync has 0KB

查看:265
本文介绍了下载的文件使用webclient.DownloadFileAsync有0KB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试下载zend-framework(从 http://framework.zend.com/releases/ZendFramework-1.11.11/ZendFramework-1.11.11.zip )只需使用WebClient

I'm trying to download zend-framework (from http://framework.zend.com/releases/ZendFramework-1.11.11/ZendFramework-1.11.11.zip) simply using WebClient

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

该文件已创建,但为空。我使用提示器检查响应,我得到HTTP 200,正确的内容长度,但连接:关闭,提琴者在正文列中显示-1。

The file is created, but it is empty. I checked response using fiddler and I get HTTP 200, correct content-length but "connection: closed" and fiddler shows "-1" in "body" column.

尝试添加用户代理(从Google Chrome请求复制)和连接:保持活着到标题,但没有一个有帮助。我也很确定,我的程序使用相同的URL一次或两次下载这个文件。 WebClient触发的事件没有错误。

I have tried adding user agent (copied from google chrome request) and "connection: keep-alive" to headers, but none of these helped. I'm also pretty sure, that my program downloaded this file using the same URL once or twice before. There are no errors in events fired by WebClient.

任何想法?

推荐答案

只是我的猜测:也许您可以尝试将WebClient实例保留在某个地方不会被垃圾回收。当DownloadFileCompleted事件触发时,您只需清理对WebClient实例的引用,并让GC稍后回收内存(不要忘记调用Dispose方法)。

Just my guess: maybe you can try to keep the WebClient instance in some place would not be garbage collected. When the DownloadFileCompleted event fired, you just clean the reference to the WebClient instance and let GC to reclaim the memory later (and don't forget to call Dispose method).

这篇关于下载的文件使用webclient.DownloadFileAsync有0KB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆