1. 程式人生 > >使用httpclient post請求中文亂碼解決辦法

使用httpclient post請求中文亂碼解決辦法

gin pri div method con 情況下 turn .get 解決

使用httpclient post請求中文亂碼解決辦法

在使用httpclient發送post請求的時候,接收端中文亂碼問題解決。

正文:

我們都知道,一般情況下使用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);

在接收(服務器)端:

技術分享圖片

主要代碼:

@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請求中文亂碼解決辦法