10

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

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?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Totti
  • 665
  • 5
  • 12
  • 26

2 Answers2

36

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"));

and

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.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
2

I don't know where word variable is comming from, but try this:

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 Java for detailed discussion.

Community
  • 1
  • 1
Tomasz Nurkiewicz
  • 334,321
  • 69
  • 703
  • 674