阅读:4932回复:0
解决HttpUrlConnection中文乱码问题
在使用HttpUrlConnection过程中,如果参数中包含中文字符,则会出现乱码,导致获取数据失败。
我们只需在传入参数前,将参数进行编码操作即可解决。 例如:String urlStr = “http://10.203.8.2:8888/xxzx/index.xhtml” 参数areacode = “四川”; 我们只需将参数进行编码, areacode = URLEncoder.encode(“四川”, "utf-8"); 即可解决HttpUrlConnection中文乱码问题!!!!! /** * 从基础数据库中获取字符数据 * @param urlTemp 数据源查询模板 * @param param 请求参数集合 * @return */ @Override public StringBuffer queryData(String urlTemp, Map<String, Object> param) { String urlStr = URLUtil.getUrlByTemp(urlTemp, param); System.out.println("read data from basicdb,url="+urlStr); URL url = null; HttpURLConnection uc = null; BufferedReader reader = null; StringBuffer buffer = new StringBuffer(); try { url = new URL(urlStr); uc = (HttpURLConnection) url.openConnection(); uc.setConnectTimeout(300000); reader = new BufferedReader(new InputStreamReader(uc.getInputStream(),"utf-8")); String line = reader.readLine(); while (line != null) { buffer.append(line).append("\r\n"); line = reader.readLine(); } reader.close(); } catch (Exception ex) { ex.printStackTrace(); } finally { if (null != reader){ try { reader.close(); } catch (IOException e) { e.printStackTrace(); } } } return buffer; } |
|