我需要将响应流写入文件。该流包含一个编码页面 (iso-8859-1)。
这是我的代码:
...
using (TextWriter writer = new StreamWriter(tmpFilePath))
{
using (TextReader reader = new StreamReader(answer, Encoding.GetEncoding("ISO-8859-1")))
{
string line = "";
while ((line = reader.ReadLine()) != null)
{
// try to decode
string decoded_line = decode(line);
writer.WriteLine(decoded_line);
}
}
}
...
string decode(string message)
{
string result = "";
Encoding iso = Encoding.GetEncoding("iso-8859-1");
Encoding utf8 = Encoding.UTF8;
byte[] isoBytes = iso.GetBytes(message);
byte[] utf8Bytes = Encoding.Convert(iso, utf8, isoBytes);
result = utf8.GetString(utf8Bytes);
return result;
}
问题是解码()不工作
如何正确将 iso-8859-1 转换为 utf8?
UPDATE
我依靠 fiddler 来获取内容类型:
鉴于这种情况,应该足以:
using (TextWriter writer = new StreamWriter(tmpFilePath, Ecoding.UTF8))
{
using (TextReader reader = new StreamReader(answer, Encoding.GetEncoding("ISO-8859-1")))
{
while ((line = reader.ReadLine()) != null)
{
writer.WriteLine(decoded_line);
}
}
}
如果这不起作用,请检查您的数据。 (第一)行在调试器中看起来如何?
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)