1

当我尝试下载文本文件时,我收到错误的文本,例如"úěć˨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);
}

file 没有问题20120603.prn,但20120601.prnis. 怎么回事?

4

3 回答 3

2

你需要这样的东西

client.Encoding = Encoding.GetEncoding("your encoding");
于 2012-06-09T12:12:44.033 回答
2

通过 WebClient.DownloadData 自动解压缩 gzip 响应的副本

基本上你必须启用webclient的自动解压。如果您检查文件 20120601.prn 的响应标头(例如使用 firebug 或 fiddler),则会返回 gzip Content-Encoding。对于文件 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; 
    } 
} 
于 2012-06-09T12:23:02.387 回答
1

将您的编码设置为 UTF8

_WebClient.Encoding = System.Text.Encoding.UTF8;
于 2012-06-09T12:16:12.140 回答