简体   繁体   中英

UTF-8 encoding problem with servlet and apache HttpClient

I have a servlet that sends a string with utf-8 encoding. Also I have a client written with apache httpcomponents library.

My problem is reading the response in utf-8. Some special characters like ñ or ç are not read correctly. If I test the server with an html page sending a request, the string is correct and the encoding is UTF-8 without BOM.

Some snippets: Servlet

response.setContentType ("application/json; charset=UTF-8");
PrintWriter out = response.getWriter ();
out.write (string);

Client

entity = response.getEntity ();
entity.getContentEncoding (); //returns null
resultado = EntityUtils.toString (entity, HTTP.UTF_8); //Some characters are wrong

Has anyone had the same problem?

SOLVED: Sorry guys the client and server were working correctly. I'm writting an android app and it seems that the logcat (where I print the messages) doesn't support utf-8 encoding.

Have you tried

response.setCharacterEncoding("utf-8");

instead of setting the encoding via setContentType ? It shouldn't make a difference according to the documentation, but who knows...

Also, make sure you didn't call response.getWriter() anywhere in your code before setting the character encoding, because the latter would not have any effect in that case.

确保流字节为UTF-8格式:

out.write((yourstring.getBytes("UTF-8"));

StandardCharsets.UTF_8 can be used with EntityUtil to get the proper encoding.

Here is a sample snippet:

HttpEntity entity = response.getEntity();
String webpage = EntityUtils.toString(entity, StandardCharsets.UTF_8);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM