HttpWebRequest编码问题

yy1987316 2011-01-13 02:52:43
一个接口
用IE访问得到的XML中汉字内容是正常的。
用程序发送HttpWebRequest,得到的XML无论怎样编码,汉字部分都是乱码。
请教高手怎么回事?
...全文
267 15 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
yy1987316 2011-01-13
  • 打赏
  • 举报
回复
问题解决了。多谢各位的热心帮助。

post的参数中如果有汉字,需要url编码转换。

province = HttpUtility.UrlEncode(province, Encoding.GetEncoding("GBK"));

孟子E章 2011-01-13
  • 打赏
  • 举报
回复
StreamReader webRstrem = new StreamReader(resStream, Encoding.Unicode);
StreamReader webRstrem = new StreamReader(resStream, Encoding.BigEndianUnicode);
StreamReader webRstrem = new StreamReader(resStream, System.Text.UnicodeEncoding.GetEncoding("GB2312"));


都试试
yy1987316 2011-01-13
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 wuyq11 的回复:]
看看流有没有被压缩,如Gzip
// 获取网页的HTML内容,指定Encoding
static string GetHtml(string url, Encoding encoding)
{
byte[] buf = new WebClient().DownloadData(url);
if (encoding != null) return encoding.GetS……
[/Quote]
如何看流是否被压缩。英文部分是没有问题的,只有汉字是乱码。

采用GetHtml(url, null);测试,没有解决问题...
yy1987316 2011-01-13
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 net_lover 的回复:]
你还可以
XmlDocument d = new XmlDocument();
d.Load("http://xxx/x.xml");
保证不乱
[/Quote]

接口我不太懂,我是发一个POST请求过去,然后按照响应接受到的,没有XML地址
孟子E章 2011-01-13
  • 打赏
  • 举报
回复
那就
XmlDocument d = new XmlDocument();
d.Load("http://xxx/x.xml");

然后
d.OuterXml得到xml的全部内容
yy1987316 2011-01-13
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 net_lover 的回复:]
StreamReader webRstrem = new StreamReader(resStream, Encoding.GetEncoding("utf-8"));
StreamReader webRstrem = new StreamReader(resStream, Encoding.GetEncoding("GB2312"));

试试就知道了
[/Quote]
各种都试过了,还是乱码
yy1987316 2011-01-13
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 net_lover 的回复:]
你的xml地址我可以访问吗
[/Quote]

不行,是内网的
孟子E章 2011-01-13
  • 打赏
  • 举报
回复
你的xml地址我可以访问吗
yy1987316 2011-01-13
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 lovezhizi 的回复:]
用HttpWebResponse接受之后 要指定他的编码 默认是utf-8
Stream resStream = MyResponse.GetResponseStream();
StreamReader webRstrem = new StreamReader(resStream, Encoding.GetEncoding("utf-8"));
[/Quote]
试过各种编码了,不管用
yy1987316 2011-01-13
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 whowhen21 的回复:]
你打开页面看看页面编码是什么。
如果utf-8的话,你去到的也要用utf-8来解码~~推之....
*************************************************
本内容使用CSDN小秘书回复
每天回帖即可得10分可用分!
*************************************************
[/Quote]
页面的编码选项是黑的(不可选),指定的编码是Unicode,但我用Unicode编码读的时候,不仅中文部分,整个XML都是乱码了,何解?
孟子E章 2011-01-13
  • 打赏
  • 举报
回复
你还可以
XmlDocument d = new XmlDocument();
d.Load("http://xxx/x.xml");
保证不乱
孟子E章 2011-01-13
  • 打赏
  • 举报
回复
StreamReader webRstrem = new StreamReader(resStream, Encoding.GetEncoding("utf-8"));
StreamReader webRstrem = new StreamReader(resStream, Encoding.GetEncoding("GB2312"));

试试就知道了
wuyq11 2011-01-13
  • 打赏
  • 举报
回复
看看流有没有被压缩,如Gzip
// 获取网页的HTML内容,指定Encoding
static string GetHtml(string url, Encoding encoding)
{
byte[] buf = new WebClient().DownloadData(url);
if (encoding != null) return encoding.GetString(buf);
string html = Encoding.UTF8.GetString(buf);
encoding = GetEncoding(html);
if (encoding == null || encoding == Encoding.UTF8) return html;
return encoding.GetString(buf);
}

// 根据网页的HTML内容提取网页的Encoding
static Encoding GetEncoding(string html)
{
string pattern = @"(?i)\bcharset=(?<charset>[-a-zA-Z_0-9]+)";
string charset = Regex.Match(html, pattern).Groups["charset"].Value;
try { return Encoding.GetEncoding(charset); }
catch (ArgumentException) { return null; }
}
lovezhizi 2011-01-13
  • 打赏
  • 举报
回复
用HttpWebResponse接受之后 要指定他的编码 默认是utf-8
Stream resStream = MyResponse.GetResponseStream();
StreamReader webRstrem = new StreamReader(resStream, Encoding.GetEncoding("utf-8"));
whowhen21 2011-01-13
  • 打赏
  • 举报
回复
你打开页面看看页面编码是什么。
如果utf-8的话,你去到的也要用utf-8来解码~~推之....
*************************************************
本内容使用CSDN小秘书回复
每天回帖即可得10分可用分!
*************************************************

111,097

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧