打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
httpclient 中文乱码
3个地方设置编码,解决乱码问题

    public static String GetResponse(HttpMethodBase Method) throws IOException {
        String charset= Method.getResponseCharSet();
        System.out.println("返回的字符编码为:"+charset);
        InputStream responseBody = Method.getResponseBodyAsStream();
//        BufferedReader br = new BufferedReader(new InputStreamReader(responseBody));
        BufferedReader br = new BufferedReader(new InputStreamReader(Method.getResponseBodyAsStream(), "ISO-8859-1"));//1
        String tempbf;
        StringBuffer htmlbf = new StringBuffer(100);
        while ((tempbf = br.readLine()) != null) {
//            htmlbf.append(tempbf);
            htmlbf.append(new String(tempbf.getBytes("iso-8859-1"),"utf-8"));//2
            htmlbf.append("\n");
        }
        return htmlbf.toString();
    }//end GetResponse

然后在构建GetMethod或PostMethod时也指定编码

          Method.addRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=utf-8");//3

或者        NameValuePair p2 = new NameValuePair( "Content-Type","text/html; charset=utf-8");

post.setRequestBody( new NameValuePair[]{p2})//3

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
JAVA中文字符编码乱码详述(非常全,详细,从文件编码,编译说起)
乱码解决
中文乱码
javascript 未结束的字符串常量
支付宝登录接口解析
使用HttpClient模拟网站登陆sina邮箱(保持远程服务器session和cookie) (转载) - 海浪 - 博客园
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服