C# 下载后的错误文本文件
当我尝试下载文本文件时,我会收到像ěěË˨TÁs 我使用WebClient类:C# 下载后的错误文本文件,c#,download,webclient,C#,Download,Webclient,当我尝试下载文本文件时,我会收到像ěěË˨TÁs 我使用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"; _WebCl
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没有问题。
怎么回事?你需要这样的东西
client.Encoding = Encoding.GetEncoding("your encoding");
你需要这样的东西
client.Encoding = Encoding.GetEncoding("your encoding");
将编码设置为UTF8
_WebClient.Encoding = System.Text.Encoding.UTF8;
将编码设置为UTF8
_WebClient.Encoding = System.Text.Encoding.UTF8;
副本
基本上,您必须启用webclient的自动解压缩。例如,如果使用文件20120601.prn的firebug或fiddler检查响应标头,则返回gzip内容编码。对于文件20120603.prn,完全缺少内容编码头
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;
}
}
副本
基本上,您必须启用webclient的自动解压缩。例如,如果使用文件20120601.prn的firebug或fiddler检查响应标头,则返回gzip内容编码。对于文件20120603.prn,完全缺少内容编码头
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;
}
}
20120603.prn中是否只有英文字母?20120603.prn中是否只有英文字母?如果我没有弄错,您的编码将是ISO8859-2如果我没有弄错,您的编码将是ISO8859-2