WebClient downloads corrupted file in windows 8.1

岁酱吖の 提交于 2019-12-11 12:26:39

问题


I have a simple code that downloads a file from specified URL and it works great in windows 7, but when i run it in windows 8.1 downloaded file is corrupted. Where is the problem?

This is the code and URL:

WebClient wClient = new WebClient();
wClient.DownloadFile(@"http://members.tsetmc.com/tsev2/excel/MarketWatchPlus.aspx?d=0", "dl.xlsx");

回答1:


This URL does not deliver what you expect. Use Fiddler to find out what happens at the HTTP level. You need to find out what the server needs as input to respond with the correct content.




回答2:


Thanks to usr for help, i found the problem, server returns the file in GZip format so i have adapted the code:

public class WebDownload : WebClient
{
    protected override WebRequest GetWebRequest(Uri address)
    {
        HttpWebRequest request = (HttpWebRequest)base.GetWebRequest(address);
        if (request != null)
        {
            request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
        }
        return request;
    }
}

But still i dont know why my initial code runs without problem in my PC!



来源:https://stackoverflow.com/questions/26206141/webclient-downloads-corrupted-file-in-windows-8-1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!