带有字符串实体的 HttpPost 具有特殊字符,如 ® ,看到 ¿1/2' 而不是 ®

我需要使用特殊字符,如下所示。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®


答案 1

更改为:

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

答案 2

而不是StringEntity,我使用的是ByteArrayEntity

String string = "ã@í";
HttpEntity httpEntity = new ByteArrayEntity(string.getBytes("UTF-8"));
request.addHeader("Content-type", "application/xml;charset=UTF-8");
request.setEntity(httpEntity);

推荐