尝试下载文本文件时,出现诸如"úěć˨Të€Ás…­žVż$—éxś¶źŹßCb}㬖92á•,˝V....."的错误文本

我使用WebClient类:

private void button1_Click(object sender, EventArgs e)
{
    WebClient _WebClient = new WebClient();
    string url = "http://bossa.pl/pub/metastock/forex/sesjafx/";
    string file= "20120601.prn";
    _WebClient.DownloadFile(url + file, @"C:\"+file);
}


文件20120603.prn没有问题,但是20120601.prn是。
怎么了

最佳答案

Automatically decompress gzip response via WebClient.DownloadData的重复项

基本上,您必须启用Web客户端的自动解压缩。如果检查文件20120601.prn的响应头(例如,使用Firebug或Fiddler),则会返回gzip内容编码。对于文件20120603.prn,根本没有Content-Encoding头。

void Main()
{
    WebClient _WebClient = new MyWebClient();
    string url = "http://bossa.pl/pub/metastock/forex/sesjafx/";
    string file= "20120601.prn";
    string a = _WebClient.DownloadString(url + file);
}

class MyWebClient : WebClient
{
    protected override WebRequest GetWebRequest(Uri address)
    {
        HttpWebRequest request = base.GetWebRequest(address) as HttpWebRequest;
        request.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;
        return request;
    }
}

关于c# - 下载后的错误文本文件,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10960821/

10-11 22:44
查看更多