使用httpclient post请求中文乱码解决办法

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/lwl20140904/article/details/86518334

public static String doPostWithJsonInString(String url, String json){
HttpClient client = getHttpClient();
HttpPost post = new HttpPost(url);
String response = null;
try {
//设置编码格式
post.addHeader(“Content-type”,“application/json;charset=utf-8”);
post.setHeader(“Accept”, “application/json”);
post.setEntity(new StringEntity(json,Charset.forName(“UTF-8”)));

HttpResponse res = client.execute(post);
if(res.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
HttpEntity entity = res.getEntity();
// 返回json格式
response = EntityUtils.toString(entity);
}
} catch (Exception e) {
throw new BaseException(e);
}
return response;
}

猜你喜欢

转载自blog.csdn.net/lwl20140904/article/details/86518334