您的位置 首页 java

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

【ps:本文为凯哥java实际工作中要到错误系列教程,在文章末尾会有本系列其他教程传送门】

概要:

在使用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;

}

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

相关推荐:

使用httpclient post请求中文乱码解决办法《maven web项目启动报错 org.springfram》

使用httpclient post请求中文乱码解决办法《使用java做爬虫获取网络资源下载403错误解决办法》

使用httpclient post请求中文乱码解决办法《解决eclipse在修改js或jsp卡顿现象》

使用httpclient post请求中文乱码解决办法《在用httpclient发送post报文请求错误解决》

使用httpclient post请求中文乱码解决办法《使用spring mvc 返回json报406错误解决》

ps:

如果您觉得本文对您有帮助,烦请您转发。谢谢!

欢迎

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

关注:【凯哥java】

文章来源:智云一二三科技

文章标题:使用httpclient post请求中文乱码解决办法

文章地址:https://www.zhihuclub.com/181656.shtml

关于作者: 智云科技

热门文章

网站地图