HttpClient post中文乱码解决
在javase方式下使用HttpClient没有进行任何编码设置,本地从服务端获取到数据不存在中文乱码。
但是将此段代码部署到Tomcat下面出现了中文乱码,此时设置:
post.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8");
还是中文乱码。我当前的Tomcat环境不是UTF-8,在进行:
BufferedReader reader = new BufferedReader( new InputStreamReader(post.getResponseBodyAsStream());
InputStreamReader将读入内容解码为平台默认编码所有此时会出现中文乱码,解决方法:
BufferedReader reader = new BufferedReader(
new InputStreamReader(post.getResponseBodyAsStream(),"UTF-8"));
if (statusCode == HttpStatus.SC_OK) { BufferedReader reader = new BufferedReader( new InputStreamReader(post.getResponseBodyAsStream()); String lineStr; while ((lineStr = reader.readLine()) != null) { builder.append(lineStr); } }