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;
}
使用httpclient post请求中文乱码解决办法
最新推荐文章于 2024-07-12 00:00:00 发布