HttpClient Get请求中返回中文乱码问题解决
来源:互联网 发布:大秦帝国 纵横 知乎 编辑:程序博客网 时间:2024/05/16 09:43
1、Get请求中中文乱码问题,在代码中修改了"utf-8"编码格式,在web.xml文件中也进行了相应的过滤"utf-8"编码格式,在tomacat中也配置了URIEncoding="UTF-8",但是在Get请求的时候,返回值依然是中文乱码。
// 远程调用userCenter中的方法 String resultSemester = HttpTool.requestGet( NxConfig.getGetSelectModelBySchoolIdJSONResult() + "?" + "clsSchoolId=" + clsSchoolId, null);
public static String requestGet(String url, Map<String, String> headers)throws IOException {return requestGet(null, url, headers, DEFAULT_CHARSET, false,DEFAULT_CONNECTION_TIMEOUT, DEFAULT_SOCKET_TIMEOUT);}
public static String requestGet(SSLContext sslContext, String url,Map<String, String> headers, String charset, boolean keepAlive,int connectTimeout, int socketTimeout) throws IOException {HttpClient httpClient = null;HttpGet get = null;HttpResponse response = null;try {// create builderHttpClientBuilder httpClientBuilder = createHttpClientBuilder(sslContext, headers, connectTimeout, socketTimeout);// invoke get requestget = new HttpGet(url);if (keepAlive) {get.addHeader("Connection", "Keep-Alive");} else {get.addHeader("Connection", "close");}logger.info("Executing get request:" + get.getURI());httpClient = httpClientBuilder.build();response = httpClient.execute(get);String res = EntityUtils.toString(response.getEntity(), charset);logger.debug("Response content from <" + get.getURI() + ">: " + res);return res;} finally {if (get != null) {get.abort();}}}
2、被远程调用的UserCenter中的方法
@ResponseBody @RequestMapping("getSelectModelBySchoolIdJSONResult") public String getSelectModelBySchoolIdJSONResult(String clsSchoolId,HttpServletResponse response) { List<SelectModel> resultList = getSelectModelBySchoolId(clsSchoolId); response.setHeader("Cache-Control", "no-cache"); response.setContentType("text/json;charset=UTF-8"); response.setCharacterEncoding("UTF-8"); try { PrintWriter out = response.getWriter(); out.write(JSONObject.toJSONString(resultList)); } catch (IOException e) { e.printStackTrace(); } return null; }
@ResponseBody@RequestMapping("getSelectModelBySchoolId")public List<SelectModel> getSelectModelBySchoolId(String clsSchoolId) {return baseSemesterService.getSelectModelBySchoolId(clsSchoolId);}
3、getSelectModelBySchoolIdJSONResult方法中加上了(HttpServletResponse response)就可以防止返回值中中文乱码的问题了。
阅读全文
0 0
- HttpClient Get请求中返回中文乱码问题解决
- httpclient返回值中文乱码问题解决方法
- httpclient返回值中文乱码问题解决方法
- httpclient返回值中文乱码问题解决方法
- HttpClient 发送Get请求 中文乱码
- get请求中有中文到后台乱码问题解决方式
- get请求返回中文乱码问题
- get请求乱码问题解决
- 解决javaee中get请求中文乱码
- tomcat中get请求中文乱码
- SpringMVC中get请求中文乱码问题
- get请求中文乱码问题解决和参数编码
- HttpClient PostMethod 中文乱码问题解决
- Get请求中文乱码
- Get请求中文乱码
- get请求中文乱码
- get请求中文乱码
- Get中文请求乱码
- 括号配对问题 (南阳理工oj-题目2)
- wcf序列化和消息编码
- 微信小程序获取系统时间、时间戳、时间时间戳加减
- mysql基础
- Linux 设置编码格式&乱码
- HttpClient Get请求中返回中文乱码问题解决
- [html5入门-27]CSS布局奇淫巧计之-强大的负边距
- LeetCode 38. Count and Say
- javascript总结
- git中Please enter a commit message to explain why this merge is necessary. Please enter a commit mess
- for,while循环,if判断格式
- 数据结构实验之栈二:一般算术表达式转换成后缀式
- opencv 摄像头/视频读取_写入
- 【总结】嵌入式Linux学习中遇到的问题及解决方法