【Java】HttpClient 请求出现中文乱码的问题

目录

一、现象

二、解决方式

2.1 指定请求数据的字符集为 utf-8 格式

2.2 通过 @RequestMapping 中的 produces 属性解决,指定接收方的响应数据字符集为 utf-8

三、原因和验证

3.1 排除请求是否是乱码

3.2 解决请求方获取到响应的数据为乱码


一、现象

在测试 HttpClient 时出现中文乱码的问题。但是在post请求的接收方中打印日志,可以看到接收方收到的数据是正确的。

请求方的日志:

接收方的日志:

 

二、解决方式

先说解决方式:

2.1 指定请求数据的字符集为 utf-8 格式

StringEntity entity = new UrlEncodedFormEntity(kvList, "utf-8");

  

 

2.2 通过 @RequestMapping 中的 produces 属性解决,指定接收方的响应数据字符集为 utf-8

@RequestMapping(value = "/httpclient/postparm", produces = MediaType.APPLICATION_JSON_VALUE+";charset=utf-8")
.....

  

 

三、原因和验证

出现中文乱码的原因是由于没有指定字符集为 utf-8

3.1 排除请求是否是乱码

先放上相关代码:

@Test
public void doPostWithParam() throws Exception {
    // 创建一个httpclient对象
    CloseableHttpClient httpClient = HttpClients.createDefault();
    // 创建一个post对象
    HttpPost post = new HttpPost("http://localhost:8083/httpclient/postparm.action");
    // 创建一个entity,模拟一个表单
    List<NameValuePair> kvList = new ArrayList<>();
    kvList.add(new BasicNameValuePair("name", "小王"));
    kvList.add(new BasicNameValuePair("pwd", "123456"));
    // 包装成一个Entity对象
    StringEntity entity = new UrlEncodedFormEntity(kvList, "utf-8");
    // 设置请求的内容
    post.setEntity(entity);

    // 执行请求
    CloseableHttpResponse response = httpClient.execute(post);
    // 得到结果
    int statusCode = response.getStatusLine().getStatusCode();
    Log.info("status = " + statusCode);
    HttpEntity resEntity = response.getEntity();
    String str = EntityUtils.toString(resEntity);
    Log.info("str = " + str);
    // 关闭httpclient
    response.close();
    httpClient.close();
}

  

尝试着去掉后再次请求:

  

  

可以得知由于在构造 entity 时已经指定了 utf-8 格式,故接收方拿到的数据不会乱码

 

3.2 解决请求方获取到响应的数据为乱码

我们可以猜测,是由于响应的数据字符集不是 utf-8 导致的。可以通过 @RequestMapping 中的 produces 属性解决

@RequestMapping(value = "/httpclient/postparm", produces = MediaType.APPLICATION_JSON_VALUE+";charset=utf-8")
@ResponseBody
public String testPostParm(String name, String pwd) {
    Log.info("testPost   Parmname = " + name);
    Log.info("testPostParm    pwd = " + pwd);
    return "{username:" + name + ",pwd:" + pwd +"}";
}

重启服务后再次访问,问题解决了

 

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
出现乱码的原因可能有很多,这里列举几种可能的解决办法: 1. 设置正确的字符编码 在使用HttpClient发送请求时,需要设置正确的字符编码,否则在接收响应时可能会出现乱码。可以通过设置请求头中的Content-Type来指定编码方式,例如设置UTF-8: ``` HttpPost httpPost = new HttpPost(url); httpPost.setHeader("Content-Type", "application/json;charset=UTF-8"); ``` 2. 使用StringEntity传输数据 如果使用HttpClient发送POST请求,并且需要传输数据,可以使用StringEntity来设置请求数据,例如: ``` HttpPost httpPost = new HttpPost(url); httpPost.setHeader("Content-Type", "application/json;charset=UTF-8"); StringEntity stringEntity = new StringEntity(jsonStr, "UTF-8"); httpPost.setEntity(stringEntity); ``` 3. 检查响应头中的Content-Type 在接收响应时,需要检查响应头中的Content-Type是否正确,例如: ``` HttpResponse httpResponse = httpClient.execute(httpPost); Header contentTypeHeader = httpResponse.getEntity().getContentType(); if (contentTypeHeader != null) { String contentType = contentTypeHeader.getValue(); if (contentType.contains("charset=GBK")) { // 使用GBK编码解析响应数据 responseStr = EntityUtils.toString(httpResponse.getEntity(), "GBK"); } else { // 使用默认编码解析响应数据 responseStr = EntityUtils.toString(httpResponse.getEntity()); } } ``` 4. 使用ByteArrayEntity传输数据 如果使用HttpClient发送POST请求,并且需要传输二进制数据,可以使用ByteArrayEntity来设置请求数据,例如: ``` HttpPost httpPost = new HttpPost(url); httpPost.setHeader("Content-Type", "application/octet-stream"); ByteArrayEntity byteArrayEntity = new ByteArrayEntity(bytes); httpPost.setEntity(byteArrayEntity); ``` 5. 检查响应数据是否压缩 如果响应数据是压缩格式(如gzip),需要先解压缩再解析数据,例如: ``` HttpResponse httpResponse = httpClient.execute(httpPost); HttpEntity entity = httpResponse.getEntity(); if (entity != null) { InputStream instream = entity.getContent(); if (entity.getContentEncoding() != null && "gzip".equalsIgnoreCase(entity.getContentEncoding().getValue())) { instream = new GZIPInputStream(instream); } responseStr = EntityUtils.toString(entity, "UTF-8"); } ``` 以上是几种可能的解决办法,具体需要根据实际情况进行调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值