servlet 和 apache HttpClient 的 UTF-8 编码问题

发布于 2024-10-07 11:27:29 字数 678 浏览 5 评论 0原文

我有一个 servlet,它发送一个 utf-8 编码的字符串。我还有一个用 apache httpcomponents 库编写的客户端。

我的问题是读取 utf-8 格式的响应。某些特殊字符(如 ñ 或 ç)无法正确读取。如果我用发送请求的html页面测试服务器,字符串是正确的,并且编码是UTF-8无BOM。

一些片段: Servlet

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

客户端

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

有人遇到过同样的问题吗?

已解决: 抱歉,客户端和服务器工作正常。我正在编写一个 Android 应用程序,似乎 logcat (我打印消息的地方)不支持 utf-8 编码。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

美人骨 2024-10-14 11:27:29

您是否尝试

response.setCharacterEncoding("utf-8");

过通过 setContentType 设置编码?根据文档,它应该没有什么区别,但谁知道呢...

另外,请确保在设置字符编码之前没有在代码中的任何位置调用 response.getWriter() ,因为在这种情况下,后者不会产生任何影响。

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.

我也只是我 2024-10-14 11:27:29

确保流字节采用 UTF-8 格式:

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

Make sure stream bytes are in UTF-8 format:

out.write((yourstring.getBytes("UTF-8"));
相思故 2024-10-14 11:27:29

StandardCharsets.UTF_8 可以与 EntityUtil 一起使用以获得正确的编码。

这是一个示例片段:

HttpEntity entity = response.getEntity();
String webpage = EntityUtils.toString(entity, StandardCharsets.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);
千年*琉璃梦 2024-10-14 11:27:29

我有一个类似的问题,我通过使用 UTF-8 编码解决,如下所示:

IOUtils.toString(response.getEntity().getContent(), Charsets.UTF_8)

命名空间:

import com.google.common.base.Charsets;

I've got a similar problem that i solved by using UTF-8 encoding as following:

IOUtils.toString(response.getEntity().getContent(), Charsets.UTF_8)

Namespace:

import com.google.common.base.Charsets;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文