java 在 HTTP 标头中发送 UTF-8 值会导致 Mojibake
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11213160/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Sending UTF-8 values in HTTP headers results in Mojibake
提问by Totti
i want to send arabic data from servlet using HTTPServletResponse
to client
我想使用从 servletHTTPServletResponse
向客户端发送阿拉伯语数据
i am trying this
我正在尝试这个
response.setCharacterEncoding("UTF-8");
response.setHeader("Info", arabicWord);
and i receive the word like this
我收到这样的词
String arabicWord = response.getHeader("Info");
in client(receiving) also tried this
在客户端(接收)也试过这个
byte[]d = response.getHeader("Info").getBytes("UTF-8");
arabicWord = new String(d);
but seems like there is no unicode because i receive strange english words,so please how can i send and receive arabic utf8 words?
但似乎没有unicode,因为我收到奇怪的英文单词,所以请问我如何发送和接收阿拉伯语utf8单词?
回答by BalusC
HTTP headers doesn't support UTF-8. They officially support ISO-8859-1 only. See also RFC 2616section 2:
HTTP 标头不支持 UTF-8。他们只正式支持 ISO-8859-1。另请参阅RFC 2616第 2 节:
Words of *TEXT MAY contain characters from character sets other than ISO- 8859-1 [22] only when encoded according to the rules of RFC 2047 [14].
仅当根据 RFC 2047 [14] 的规则进行编码时,*TEXT 的单词才可能包含来自除 ISO-8859-1 [22] 以外的字符集的字符。
Your best bet is to URL-encode and decode them.
最好的办法是对它们进行 URL 编码和解码。
response.setHeader("Info", URLEncoder.encode(arabicWord, "UTF-8"));
and
和
String arabicWord = URLDecoder.decode(response.getHeader("Info"), "UTF-8");
URL-encoding will transform them into %nn
formatwhich is perfectly valid ISO-8859-1. Note that the data sent in the headers may have size limitations. Rather send it in the response body instead, in plain text, JSON, CSV or XML format. Using custom HTTP headers this way is namely a design smell.
URL 编码会将它们转换为完全有效的 ISO-8859-1%nn
格式。请注意,在标头中发送的数据可能有大小限制。而是在响应正文中以纯文本、JSON、CSV 或 XML 格式发送它。以这种方式使用自定义 HTTP 标头是一种设计风格。
回答by Tomasz Nurkiewicz
I don't know where word
variable is comming from, but try this:
我不知道word
变量来自哪里,但试试这个:
arabicWord = new String(d, "UTF-8");
UPDATE: Looks like the problem is with UTF-8 encoded data in HTTP headers, see: HTTP headers encoding/decoding in Javafor detailed discussion.
更新:看起来问题在于 HTTP标头中的UTF-8 编码数据,请参阅:Java 中的 HTTP 标头编码/解码以获取详细讨论。