java - 带有 StringEntity 的 HttpPost 具有特殊字符,如 ®,看到 ¿½` 而不是 ®

标签 java character-encoding apache-httpclient-4.x

我需要为 stringentity 使用特殊字符,如下所示。

DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpEntity entity = new StringEntity("test®");
httpPost.setEntity(entity);
httpPost.setHeader("Accept-Encoding", "UTF-8");

HttpResponse response = httpClient.execute(httpPost);
BufferedReader reader =  new BufferedReader(new InputStreamReader((response.getEntity().getContent())));
while ((reader.readLine()) != null) {
    System.out.println (reader.readLine());
}
reader.close();

输出在响应中包含 test� 而不是 test®

最佳答案

更改为:

    HttpEntity entity = new StringEntity("test®", "UTF-8"); 

关于java - 带有 StringEntity 的 HttpPost 具有特殊字符,如 ®,看到 ¿½` 而不是 ®,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20473848/

相关文章:

c++ - C++中的字符串和字符编码

Python 解码/编码问题

java - Tomcat 管理器脚本部署大型 war 不工作

java - httpclient 可以使用当前登录用户自动创建授权 header 作为 java URLConnection 吗?

java - Java 中 Math.sqrt 的最坏情况时间复杂度

java - 从一组唯一数字到另一组唯一数字的单向函数

java - Java中如何从父类(super class)访问子类?

java - 在 android TextView 中检测新行

java - 字符编码

Java - 使用 Apache httpclient 的 HTTPS 基本身份验证