2011-06-15 37 views
3

每当我使用WebClient.DownloadFile()时,生成的文件长度总是0字节。我试过从不同网站上的文件,包括我自己的IIS本地文件,总是得到一个0字节的文件。在浏览器(Chrome)中点击文件名时,文件会正确下载。DownloadFile创建0字节的文件

string fileName = @"us_ysera_tier11.json.gz"; 
string remoteUri = @"http://wowprogress.com/exports/ranks/" + fileName; 

if (!File.Exists(fileName)) 
{ 
    using (WebClient webClient = new WebClient()) 
    { 
     webClient.UseDefaultCredentials = true; 
     webClient.DownloadFile(remoteUri, fileName); 
    } 
} 

我是在做一些普遍错误的事,或者有人能指点我一个工作的例子吗?

+0

查看您的代理。 – leppie 2011-06-15 14:41:40

+0

没有使用代理。 – 2011-06-15 14:44:12

+0

如果您将文件名更改为网站不喜欢的内容,并且您运行完全相同的代码,则会下载145kb的html错误文件。所以代码本身能够下载这些信息。尝试发送与chrome相同的http头(可以通过运行Fiddler或等效工具来查看它们)。 – 2011-06-15 15:06:12

回答

5

此代码在我的机器上下载5K文件。我更新了文件名和remoteUri值。

string fileName = "us_ysera_tier11.json.gz"; 
string remoteUri = "http://www.wowprogress.com/export/ranks/" + fileName; 
WebClient webClient = new WebClient(); 
webClient.Headers["Accept-Encoding"] = "application/x-gzip"; 
webClient.DownloadFile(remoteUri, fileName); 
+0

添加的标题也在我的工作上。 – 2011-06-15 16:13:29