site:mp.weixin.qq.com utf
微信卡券接口说明 - 微信公众平台开发者文档
所有API接口POST的数据只支持utf8编码,否则会返回报错。
以上是获取的部分信息
这个尽管有点模糊,只针对卡卷接口,但也能提供线索。
分析文本消息
<xml> <ToUserName><![CDATA[toUser]]></ToUserName> <FromUserName><![CDATA[fromUser]]></FromUserName> <CreateTime>1348831860</CreateTime> <MsgType><![CDATA[text]]></MsgType> <Content><![CDATA[this is a test]]></Content> <MsgId>1234567890123456</MsgId> </xml>
假设Content中的内容是utf-8编码的文本,<![CDATA[]]> 保证原样输出
具体例子:
<xml><ToUserName><![CDATA[gh_3440a78298be]]></ToUserName>
<FromUserName><![CDATA[ojpoHwQJaUYXOsKJTgeE4_yHwpZM]]></FromUserName>
<CreateTime>1469182663</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[你敢]]></Content>
<MsgId>6310091489858779233</MsgId>
</xml>
通过 HttpServletRequest 获取到 inputstream
通过read() 方法获取到整个流
byte[] bArray = {60,120,109,108,62,60,84,111,85,115,101,114,78,97,109,101,62,60,33,91,67,68,65,84,65,91,103,104,95,51,52,52,48,97,55,56,50,57,56,98,101,93,93,62,60,47,84,111,85,115,101,114,78,97,109,101,62,10,60,70,114,111,109,85,115,101,114,78,97,109,101,62,60,33,91,67,68,65,84,65,91,111,106,112,111,72,119,81,74,97,85,89,88,79,115,75,74,84,103,101,69,52,95,121,72,119,112,90,77,93,93,62,60,47,70,114,111,109,85,115,101,114,78,97,109,101,62,10,60,67,114,101,97,116,101,84,105,109,101,62,49,52,54,57,49,56,50,54,54,51,60,47,67,114,101,97,116,101,84,105,109,101,62,10,60,77,115,103,84,121,112,101,62,60,33,91,67,68,65,84,65,91,116,101,120,116,93,93,62,60,47,77,115,103,84,121,112,101,62,10,60,67,111,110,116,101,110,116,62,60,33,91,67,68,65,84,65,91,(byte) 228,(byte) 189,(byte) 160,(byte) 230,(byte) 149,(byte) 162,93,93,62,60,47,67,111,110,116,101,110,116,62,10,60,77,115,103,73,100,62,54,51,49,48,48,57,49,52,56,57,56,53,56,55,55,57,50,51,51,60,47,77,115,103,73,100,62,10,60,47,120,109,108,62};
试验各种编码
new String(bArray,"utf-8");
获取到原始文本
由此可知,该流是utf-8编码
设置 HttpServletResponse 编码
response.setCharacterEncoding("utf-8");
这个必须设置,response 默认编码是ISO-8859-1
详情见 ServletResponse.class 注释
If no charset is specified, ISO-8859-1 will be used.
git@github.com:witaste/ergouzi.git