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

2021-06-12 08:02

阅读:684

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

public static String doPost(String urlStr, Map 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


评论


亲,登录后才可以留言!