使用httpclient post请求中文乱码解决办法

在使用httpclient发送post请求的时候,接收端中文乱码问题解决。

正文:

我们都知道,一般情况下使用post请求是不会出现中文乱码的。可是在使用httpclient发送post请求报文含中文的时候在发送端数据正常但是到了服务器端就中文乱码了。

解决办法:

发送端进行设置编码如下:

使用httpclient post请求中文乱码解决办法

主要代码:

if (null != jsonParam) {

//解决中文问题。

method.addHeader("Content-type","application/json; charset=utf-8");

method.setHeader("Accept", "application/json");

method.setEntity(new StringEntity(jsonParam.toString(), Charset.forName("UTF-8")));

}

HttpResponse result = httpClient.execute(method);

在接收(服务器)端:

使用httpclient post请求中文乱码解决办法

主要代码:

@RequestMapping(value = "getJson")

@ResponseBody

public Map<String,Object> getJson(@RequestBody String requestBody, HttpServletRequest request){

requestBody = new String(requestBody.getBytes(), Charset.forName("utf-8"));

JSONObject jsonObject = JSONObject.parseObject(requestBody);

System.out.println(jsonObject);

ResultJsonInfo info = JSONObject.parseObject(jsonObject.toJSONString(), ResultJsonInfo.class);

System.out.println(info);

//TODO 处理自己业务

JSONObject result= new JSONObject();

result.put("success", "true");

Map<String, Object> resultMap = new HashMap<String, Object>();

resultMap.put("isok", true);

return resultMap;

}

这样处理之后。再次请求。乱码问题解决。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值