Totti Totti - 1 year ago 46
Java Question

Sending UTF-8 values in HTTP headers results in Mojibake

i want to send arabic data from servlet using

to client

i am trying this

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?

Answer Source

HTTP headers doesn't support UTF-8. They officially support ISO-8859-1 only. See also RFC 2616 section 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].

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-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.