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

Posted lvlv岁月流逝

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HttpURLConnection 返回汉字乱码(全是问号)相关的知识,希望对你有一定的参考价值。

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 返回汉字乱码(全是问号)的主要内容,如果未能解决你的问题,请参考以下文章

Springmvc完成ajax功能,controller类返回的数据类型为String且为汉字时出现乱码

Django 集成redis 中文汉字乱码

HttpURLConnection 接收网络数据出现乱码问题

android http通信之HttpURLConnection post乱码问题

imp后表中汉字正常,存储过程汉字乱码,如何解决?和exp时字符集一样的

android HttpURLConnection 中post 上传表单,在数据库中最后一个字段出现乱码