java post请求设置body中文乱码问题
2017-01-16 16:11
375 查看
public static String postBody(String url, String body) {
// 实例化httpClient
CloseableHttpClient httpclient = HttpClients.createDefault();
// 实例化post方法
HttpPost httpPost = new HttpPost(url);
// 结果
CloseableHttpResponse response = null;
String content = null;
httpPost.setHeader("Content-Type" , "application/json");
try {
// 将参数给post方法
if (body != null) {
StringEntity stringEntity = new StringEntity(body , "UTF-8");
httpPost.setEntity(stringEntity);
}
// 执行post方法
response = httpclient.execute(httpPost);
if (response.getStatusLine().getStatusCode() == 200) {
try {
content = EntityUtils.toString(response.getEntity(), "UTF-8");
} finally {
response.close();
}
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
httpclient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return content;
}
// 实例化httpClient
CloseableHttpClient httpclient = HttpClients.createDefault();
// 实例化post方法
HttpPost httpPost = new HttpPost(url);
// 结果
CloseableHttpResponse response = null;
String content = null;
httpPost.setHeader("Content-Type" , "application/json");
try {
// 将参数给post方法
if (body != null) {
StringEntity stringEntity = new StringEntity(body , "UTF-8");
httpPost.setEntity(stringEntity);
}
// 执行post方法
response = httpclient.execute(httpPost);
if (response.getStatusLine().getStatusCode() == 200) {
try {
content = EntityUtils.toString(response.getEntity(), "UTF-8");
} finally {
response.close();
}
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
httpclient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return content;
}
相关文章推荐
- java实现HTTP的post请求 json格式中文乱码问题
- java实现HTTP的post请求 json格式中文乱码问题
- java web,charset编码设置正确情况下出现部分中文正常,部分中文乱码的问题
- java web,charset编码设置正确情况下出现部分中文正常,部分中文乱码的问题
- java web,charset编码设置正确情况下出现部分中文正常,部分中文乱码的问题
- spring mvc 访问不同的目录下的jsp视图及处理POST请求参数中文乱码问题
- java web,charset编码设置正确情况下出现部分中文正常,部分中文乱码的问题
- java web,charset编码设置正确情况下出现部分中文正常,部分中文乱码的问题
- 解决用Spring框架的javaWeb项目post请求方法乱码的问题
- java中form以post、get方式提交数据中文乱码问题总结
- Get,Post请求中文乱码问题有效解决方法
- Struts2中解决POST请求和GET请求中的中文乱码问题
- java web,charset编码设置正确情况下出现部分中文正常,部分中文乱码的问题
- java web,charset编码设置正确情况下出现部分中文正常,部分中文乱码的问题
- Java : eclipse 编码设置 中文注释乱码问题
- java_javadoc 生成项目文档(设置编码解决中文乱码问题)
- Struts2中解决POST请求和GET请求中的中文乱码问题
- java web,charset编码设置正确情况下出现部分中文正常,部分中文乱码的问题
- Java jsp/servlet GET/POST 字符集编码问题(中文乱码)
- Get,Post请求中文乱码问题有效解决方法