码迷,mamicode.com
首页 > Web开发 > 详细

HttpURLConnection 返回汉字乱码(全是问号)

时间:2017-08-05 06:59:24      阅读:1628      评论:0      收藏:0      [点我收藏+]

标签:return   res   cache   bytes   throw   point   code   mozilla   readline   

public static String doPost(String urlStr, Map<String, Object> paramMap) throws Exception {
        URL url = new URL(urlStr);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod(SERVLET_POST);
        String paramStr = prepareParam(paramMap);
        conn.setRequestProperty("Content-type", "application/x-www-form-urlencoded");
        conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)");
        conn.setRequestProperty("Accept", "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-powerpoint, application/vnd.ms-excel, application/msword, */*");
        conn.setRequestProperty("Accept-Language", "zh-cn");
        conn.setRequestProperty("UA-CPU", "x86");
        conn.setUseCaches(false);
        conn.setConnectTimeout(6 * 1000);
        conn.setReadTimeout(6 * 1000);
        conn.setDoOutput(true);
        conn.setDoInput(true);
        conn.setRequestProperty("Charset", "utf-8");
        OutputStream os = conn.getOutputStream();
        os.write(paramStr.toString().getBytes("utf-8"));
        os.close();

        String result = "";
        if (conn.getResponseCode() == conn.HTTP_OK) {
            BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"));
            String line;
            while ((line = br.readLine()) != null) {
                result += line;
            }
            System.out.println(result);
            br.close();
            return result;
        }
        return result;
    }

 

HttpURLConnection 返回汉字乱码(全是问号)

标签:return   res   cache   bytes   throw   point   code   mozilla   readline   

原文地址:http://www.cnblogs.com/lvlv/p/7288365.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!