1

When I try download text file, I get bad text like "úěć˨Të€Ás…­žVż$—éxś¶źŹßCb}㬖92á•,˝V....."

I use WebClient class:

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);
}

There is not problem with file 20120603.prn, but with 20120601.prn is. What is going?

thomasfedb
  • 5,990
  • 2
  • 37
  • 65
Wojciech
  • 39
  • 2
  • 5

3 Answers3

2

You need something like this

client.Encoding = Encoding.GetEncoding("your encoding");
Likurg
  • 2,742
  • 17
  • 22
2

Duplicate of Automatically decompress gzip response via WebClient.DownloadData

basically you have to enable automatic decompression of the webclient. If you examine the responseheaders (forexample by using firebug or fiddler) for file 20120601.prn a gzip Content-Encoding is returned. For file 20120603.prn that Content-Encoding header is missing at all.

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; 
    } 
} 
Community
  • 1
  • 1
rene
  • 41,474
  • 78
  • 114
  • 152
1

set your encoding to UTF8

_WebClient.Encoding = System.Text.Encoding.UTF8;
Damith
  • 62,401
  • 13
  • 102
  • 153