HttpClient发起请求中文数据乱码问题解决

原来部分代码如下:
CloseableHttpClient client = HttpClients.createDefault();
HttpPost post = new HttpPost(url);
try {
StringEntity s = new StringEntity(jsonStr);
s.setContentEncoding("UTF-8");
s.setContentType("application/json");//发送json数据需要设置contentType
post.setEntity(s);
CloseableHttpResponse res = client.execute(post);

修改如下,在初始化StringEntity时指定UTF-8:
CloseableHttpClient client = HttpClients.createDefault();
HttpPost post = new HttpPost(url);
try {
StringEntity s = new StringEntity(jsonStr,"UTF-8");
s.setContentEncoding("UTF-8");
s.setContentType("application/json");//发送json数据需要设置contentType
post.setEntity(s);
CloseableHttpResponse res = client.execute(post);

posted @ 2019-01-30 14:59  知了肥  阅读(5802)  评论(0编辑  收藏  举报