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

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

正文:服务器

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

解决办法:post

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

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

主要代码:blog

if (null != jsonParam) {utf-8

//解决中文问题。get

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

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

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;

}

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

相关文章
相关标签/搜索