如何解决使用HttpClient传递JSON数据时出现的乱码问题?

2026-04-19 17:444阅读0评论SEO资源
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计767个文字,预计阅读时间需要4分钟。

如何解决使用HttpClient传递JSON数据时出现的乱码问题?

今天使用HttpClient传输JSON数据,服务端接收数据+中文乱码,下面分别贴上修改前与修改后的代码以及原因分析:(1)修改前:

javapublic String sendHttpPost(String httpUrl, String data) { // 创建HttpClient对象 HttpClient httpClient=new HttpClient(); // 创建Post方法实例 HttpPost httpPost=new HttpPost(httpUrl); // 设置请求头 httpPost.setHeader(Content-Type, application/json;charset=UTF-8); // 创建输入流 StringEntity entity=new StringEntity(data, UTF-8); // 设置请求体 httpPost.setEntity(entity); // 执行请求并获取响应 HttpResponse response=httpClient.execute(httpPost); // 获取响应字符串 String result=EntityUtils.toString(response.getEntity(), UTF-8); return result;}

如何解决使用HttpClient传递JSON数据时出现的乱码问题?

(2)修改后:

javapublic String sendHttpPost(String httpUrl, String data) { // 创建HttpClient对象 HttpClient httpClient=HttpClientBuilder.create().build(); // 创建Post方法实例 HttpPost httpPost=new HttpPost(httpUrl); // 设置请求头 httpPost.setHeader(Content-Type, application/json;charset=UTF-8); // 创建输入流 StringEntity entity=new StringEntity(data, UTF-8); // 设置请求体 httpPost.setEntity(entity); // 执行请求并获取响应 CloseableHttpClient client=HttpClientBuilder.create().build(); CloseableHttpResponse response=client.execute(httpPost); // 获取响应字符串 String result=EntityUtils.toString(response.getEntity(), UTF-8); return result;}

原因分析:

1.修改前代码中直接使用`HttpClient`类创建HttpClient对象,而修改后使用`HttpClientBuilder.create().build()`创建HttpClient对象,这样做可以更好地控制HttpClient的配置。

2.修改前代码中直接使用`httpClient.execute(httpPost)`执行请求,而修改后使用`client.execute(httpPost)`执行请求,这样做可以避免在执行请求后关闭HttpClient对象,提高代码的可重用性。

3.修改后代码中添加了`CloseableHttpClient client=HttpClientBuilder.create().build();`语句,用于创建CloseableHttpClient对象,以便在请求执行完毕后可以关闭连接。

今天用localhost:8080/qc").setEntity(stringEntity).build(); httpResponse=httpClient.execute(httpUriRequest); System.out.println("发送"); int statusCode=httpResponse.getStatusLine().getStatusCode(); if(statusCode== HttpStatus.SC_OK){ // HttpEntity entity = httpResponse.getEntity(); // InputStream in =entity.getContent(); System.out.println("文件传输服务器正常响应!"); } } catch (JsonProcessingException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }

服务端

采用最原始的servlet

import org.apache.http.protocol.HTTP; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.URLDecoder; /** * Created by 小省. */ public class QcServlet extends javax.servlet.http.HttpServlet { protected void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException { doGet(request,response); } protected void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException { System.out.println("+++++++++++++++++++"); //解决中文乱码 BufferedReader br =new BufferedReader(new InputStreamReader(request.getInputStream(),"UTF-8")); String line=null; StringBuffer sb =new StringBuffer(); while ((line=br.readLine())!=null){ sb.append(line); } System.out.println("sb.toString()"+sb.toString()); //就目前而言String reesult = URLDecoder.decode(sb.toString(), HTTP.UTF_8);是可有可无的,httpclient会自动解码 //String reesult =sb.toString(); String reesult = URLDecoder.decode(sb.toString(), HTTP.UTF_8); try { //将string 字符串转化为json数组,并且遍历 JSONObject jsonObject =new JSONObject(reesult); String mesage=(String) jsonObject.getString("data"); JSONArray myJsonArray = new JSONArray(mesage); for(int i=0 ; i < myJsonArray.length() ;i++){ //获取每一个JsonObject对象 JSONObject myjObject = myJsonArray.getJSONObject(i); System.out.println(myjObject.getString("name")); } System.out.println(reesult); } catch (JSONException e) { e.printStackTrace(); } } }

以上为个人经验,希望能给大家一个参考,也希望大家多多支持易盾网络。如有错误或未考虑完全的地方,望不吝赐教。

本文共计767个文字,预计阅读时间需要4分钟。

如何解决使用HttpClient传递JSON数据时出现的乱码问题?

今天使用HttpClient传输JSON数据,服务端接收数据+中文乱码,下面分别贴上修改前与修改后的代码以及原因分析:(1)修改前:

javapublic String sendHttpPost(String httpUrl, String data) { // 创建HttpClient对象 HttpClient httpClient=new HttpClient(); // 创建Post方法实例 HttpPost httpPost=new HttpPost(httpUrl); // 设置请求头 httpPost.setHeader(Content-Type, application/json;charset=UTF-8); // 创建输入流 StringEntity entity=new StringEntity(data, UTF-8); // 设置请求体 httpPost.setEntity(entity); // 执行请求并获取响应 HttpResponse response=httpClient.execute(httpPost); // 获取响应字符串 String result=EntityUtils.toString(response.getEntity(), UTF-8); return result;}

如何解决使用HttpClient传递JSON数据时出现的乱码问题?

(2)修改后:

javapublic String sendHttpPost(String httpUrl, String data) { // 创建HttpClient对象 HttpClient httpClient=HttpClientBuilder.create().build(); // 创建Post方法实例 HttpPost httpPost=new HttpPost(httpUrl); // 设置请求头 httpPost.setHeader(Content-Type, application/json;charset=UTF-8); // 创建输入流 StringEntity entity=new StringEntity(data, UTF-8); // 设置请求体 httpPost.setEntity(entity); // 执行请求并获取响应 CloseableHttpClient client=HttpClientBuilder.create().build(); CloseableHttpResponse response=client.execute(httpPost); // 获取响应字符串 String result=EntityUtils.toString(response.getEntity(), UTF-8); return result;}

原因分析:

1.修改前代码中直接使用`HttpClient`类创建HttpClient对象,而修改后使用`HttpClientBuilder.create().build()`创建HttpClient对象,这样做可以更好地控制HttpClient的配置。

2.修改前代码中直接使用`httpClient.execute(httpPost)`执行请求,而修改后使用`client.execute(httpPost)`执行请求,这样做可以避免在执行请求后关闭HttpClient对象,提高代码的可重用性。

3.修改后代码中添加了`CloseableHttpClient client=HttpClientBuilder.create().build();`语句,用于创建CloseableHttpClient对象,以便在请求执行完毕后可以关闭连接。

今天用localhost:8080/qc").setEntity(stringEntity).build(); httpResponse=httpClient.execute(httpUriRequest); System.out.println("发送"); int statusCode=httpResponse.getStatusLine().getStatusCode(); if(statusCode== HttpStatus.SC_OK){ // HttpEntity entity = httpResponse.getEntity(); // InputStream in =entity.getContent(); System.out.println("文件传输服务器正常响应!"); } } catch (JsonProcessingException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }

服务端

采用最原始的servlet

import org.apache.http.protocol.HTTP; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.URLDecoder; /** * Created by 小省. */ public class QcServlet extends javax.servlet.http.HttpServlet { protected void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException { doGet(request,response); } protected void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException { System.out.println("+++++++++++++++++++"); //解决中文乱码 BufferedReader br =new BufferedReader(new InputStreamReader(request.getInputStream(),"UTF-8")); String line=null; StringBuffer sb =new StringBuffer(); while ((line=br.readLine())!=null){ sb.append(line); } System.out.println("sb.toString()"+sb.toString()); //就目前而言String reesult = URLDecoder.decode(sb.toString(), HTTP.UTF_8);是可有可无的,httpclient会自动解码 //String reesult =sb.toString(); String reesult = URLDecoder.decode(sb.toString(), HTTP.UTF_8); try { //将string 字符串转化为json数组,并且遍历 JSONObject jsonObject =new JSONObject(reesult); String mesage=(String) jsonObject.getString("data"); JSONArray myJsonArray = new JSONArray(mesage); for(int i=0 ; i < myJsonArray.length() ;i++){ //获取每一个JsonObject对象 JSONObject myjObject = myJsonArray.getJSONObject(i); System.out.println(myjObject.getString("name")); } System.out.println(reesult); } catch (JSONException e) { e.printStackTrace(); } } }

以上为个人经验,希望能给大家一个参考,也希望大家多多支持易盾网络。如有错误或未考虑完全的地方,望不吝赐教。