HttpPost with StringEntity having special characters like ®, seeing ¿½` instead of ®

早过忘川 提交于 2019-12-22 03:43:35

问题


I need to use special characters for stringentity as below.

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

The output contains test� instead of test® in the response.


回答1:


Change to:

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


来源:https://stackoverflow.com/questions/20473848/httppost-with-stringentity-having-special-characters-like-seeing-%c2%bd-instead

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!