> ## Content Index
> Fetch the complete content index at: https://blog.skitisu.com/llms.txt
> Use this file to discover other available public pages before exploring further.

# 解决C#获取的HTTP Headers的中文为乱码（获取下载地址的真实文件名）
- URL: https://blog.skitisu.com/solve-csharp-http-headers-chinese-messy-code/
- Published: 2014-04-25T13:50:15.000Z
- Updated: 2014-04-25T13:50:15.000Z
- Author: SkiTiSu
- Tags: 探索, 编程, #Import 2026-10-21 14:41

由于MySQL数据库默认使用iso-8859-1编码方式，所以获取的Headers通常是使用iso-8859-1进行编码的，但由于Linux服务器通常使用UTF-8编码中文，我们只要对获得的string进行两次重编码即可，具体C#代码如下：  

HttpWebRequest Myrq = (HttpWebRequest)HttpWebRequest.Create(URL); //URL为网址
HttpWebResponse myrp = (HttpWebResponse)Myrq.GetResponse();
WebHeaderCollection whc = myrp.Headers;

Encoding e1 = Encoding.GetEncoding("iso-8859-1");
Encoding e2 = Encoding.Default;
string s = whc[1]; //这里获取第二个属性来举例
byte[] ss = e1.GetBytes(s);
byte[] sss = Encoding.Convert(Encoding.UTF8, e2, ss); //再次用UTF-8编码

string t = e2.GetString(sss);//正常的字符串
t = t.Substring(t.IndexOf("\"") + 1, t.LastIndexOf("\"") - t.IndexOf("\"") - 1);//去掉其它内容

也不排除有些网站用的是其它编码，在上文中只需修改e1即可，但是英文不管什么编码肯定是没有问题的。

参考资料：[http://shadowlin.iteye.com/blog/706744](http://shadowlin.iteye.com/blog/706744?ref=blog.skitisu.com)（JAVA中的实现）