HTTPClient POST发送JSON数据 中文无乱码
2017-07-14 11:05
627 查看
**
* post请求
* @param url
* @param json
* @return
*/
public static JSONObject doPost(String url,JSONObject json){
CloseableHttpClient httpclient = HttpClientBuilder.create().build();
HttpPost post = new HttpPost(url);
JSONObject response = null;
try {
StringEntity s = new StringEntity(json.toString(),“UTF-8”);
s.setContentEncoding("UTF-8");
s.setContentType("application/json");//发送json数据需要设置contentType
post.setEntity(s);
HttpResponse res = httpclient.execute(post);
if(res.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
String result = EntityUtils.toString(res.getEntity());// 返回json格式:
response = JSONObject.fromObject(result);
}
} catch (Exception e) {
throw new RuntimeException(e);
}
return response;
}
new StringEntity中添加“UTF-8”编码设置
* post请求
* @param url
* @param json
* @return
*/
public static JSONObject doPost(String url,JSONObject json){
CloseableHttpClient httpclient = HttpClientBuilder.create().build();
HttpPost post = new HttpPost(url);
JSONObject response = null;
try {
StringEntity s = new StringEntity(json.toString(),“UTF-8”);
s.setContentEncoding("UTF-8");
s.setContentType("application/json");//发送json数据需要设置contentType
post.setEntity(s);
HttpResponse res = httpclient.execute(post);
if(res.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
String result = EntityUtils.toString(res.getEntity());// 返回json格式:
response = JSONObject.fromObject(result);
}
} catch (Exception e) {
throw new RuntimeException(e);
}
return response;
}
new StringEntity中添加“UTF-8”编码设置
相关文章推荐
- HttpPost发送JSON数据中文乱码问题。
- HttpClient使用post和get时Json中文乱码解决方案
- tornado AsynHTTPClient.fetch() 发送POST json数据
- httpClient post发送json数据
- HttpClient通过Post方式发送Json数据
- httpclient post发送json数组并解决json乱码问题
- [置顶] HttpClient通过Post方式发送Json数据
- HttpClient和HttpUrlConnection实现post发送和接收json数据
- Http Post请求提交json格式数据工具类,解决请求参数中文乱码问题
- java http 给 c++发送json数据中文乱码问题
- android HttpPost传JSON数据中文乱码的解决方法
- HttpClient通过Post方式发送Json数据
- httpclient使用post方式发送json数据,以及服务端的接收
- spring mvc 发送http post请求返回的中文数据乱码
- httpClient 发送 post请求 json数据
- Java HttpClient Post请求发送数据乱码
- spring mvc 发送http post请求返回的中文数据乱码
- httpclient post 发送Json数据
- httpClient4发送gzip的post数据,servlet接收并解压
- Android开发中HTTP协议HttpPost向服务器发送JSon数据格式的数据