HttpClient post请求中文乱码问题解决

最近接到现场同事反馈,在掉接口的过程中,厂家收到的请求报文中文是乱码的。我检查了版控的代码,找到如下解决办法:

HttpPost httpPost = new HttpPost(url);
DefaultHttpClient httpClient = new DefaultHttpClient();
//请求头
httpPost.setHeader("Accept", MediaType.APPLICATION_JSON);
httpPost.setHeader("Content-Type", "application/json;charset=UTF-8");
//请求实体
StringEntity reqEntity = new StringEntity(reqStr);
httpPost.setEntity(reqEntity);
//获取响应
HttpResponse httpResp = httpClient.execute(httpPost);
HttpEntity respEntity = httpResp.getEntity();

解决办法:

方法一:

//请求实体
HttpEntity reqEntity = new ByteArrayEntity(reqStr.getBytes("UTF-8"));
//StringEntity reqEntity = new StringEntity(reqStr);
httpPost.setEntity(reqEntity);

方法二:

//请求实体
StringEntity reqEntity = new StringEntity(reqStr,Charset.forName("UTF-8"));
httpPost.setEntity(reqEntity);