本文介绍了在HTTP标头中发送UTF-8值会导致Mojibake的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 HttpServletResponse的从servlet的发送阿拉伯语数据的客户端

i want to send arabic data from servlet using HTTPServletResponse to client

我试图这样

response.setCharacterEncoding("UTF-8");
response.setHeader("Info", arabicWord);

我收到这样的单词

String arabicWord = response.getHeader("Info");

在客户端(接收)也试过了这个

in client(receiving) also tried this

byte[]d = response.getHeader("Info").getBytes("UTF-8");
arabicWord = new String(d);

但是好像没有unicode,因为我收到奇怪的英文单词,所以请如何发送和接收阿拉伯语的utf8文字?

but seems like there is no unicode because i receive strange english words,so please how can i send and receive arabic utf8 words?

推荐答案

HTTP标头不支持UTF-8。他们只正式支持ISO-8859-1。也参见 :

HTTP headers doesn't support UTF-8. They officially support ISO-8859-1 only. See also RFC 2616 section 2:

的*文本可以包含从字符的字符集词比ISO其他 - 8859-1 [22]仅当根据RFC 2047 [14]的规则编码时。

您最好的选择是URL -encode和对它们进行解码。

Your best bet is to URL-encode and decode them.

response.setHeader("Info", URLEncoder.encode(arabicWord, "UTF-8"));

String arabicWord = URLDecoder.decode(response.getHeader("Info"), "UTF-8");

URL编码会将它们转换为,这是完全有效的ISO-8859-1。请注意,标题中发送的数据可能有大小限制。而是将其发送到响应正文中,而不是纯文本格式,JSON格式,CSV格式或XML格式。使用自定义HTTP标头这种方式就是一种设计气味。

URL-encoding will transform them into %nn format which 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.

这篇关于在HTTP标头中发送UTF-8值会导致Mojibake的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 23:50