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

查看:37
本文介绍了使用 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.

我尝试将用户代理(从谷歌浏览器请求复制)和连接:保持活动"添加到标题,但这些都没有帮助.我也很确定,我的程序之前使用相同的 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天全站免登陆