下载后文本文件错误

时间:2012-06-09 12:00:51

标签: c# download webclient

当我尝试下载文本文件时,会收到"úěć˨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是。 怎么回事?

3 个答案:

答案 0 :(得分:2)

你需要这样的东西

client.Encoding = Encoding.GetEncoding("your encoding");

答案 1 :(得分:2)

重复Automatically decompress gzip response via WebClient.DownloadData

基本上你必须启用webclient的自动解压缩。如果您检查文件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; 
    } 
} 

答案 2 :(得分:1)

将您的编码设置为UTF8

_WebClient.Encoding = System.Text.Encoding.UTF8;