问题
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