zoukankan      html  css  js  c++  java
  • 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;
        }
  • 相关阅读:
    SQL进程死锁排查
    SQL 日期转换
    SQL Server 删除日志文件
    SQL 修复表
    charindex函数--->检索字符在字符串中的起始位置
    SQL使用链接服务器执行远程数据库上的存储过程
    C# 学习第二天笔记
    C# 学习笔记第一天
    SQL Prompt 5 功能按键说明
    自定义排序(Icompare)
  • 原文地址:https://www.cnblogs.com/lvlv/p/7288365.html
Copyright © 2011-2022 走看看