zoukankan      html  css  js  c++  java
  • http post

        public static boolean sendPostRequest(String path, String data) throws Exception{
    //        StringBuilder sb = new StringBuilder();
    //        if (params != null && !params.isEmpty()) {
    //            for (Map.Entry<String, String> entry : params.entrySet()) {
    //                sb.append(entry.getKey()).append('=').append(URLEncoder.encode(entry.getValue(), enc)).append('&');
    //            }
    //            sb.deleteCharAt(sb.length() - 1);
    //        }
            byte[] entitydata = data.getBytes();//得到实体的二进制数据
            URL url = new URL(path);
            HttpURLConnection conn = (HttpURLConnection)url.openConnection();
            conn.setRequestMethod("POST");
            conn.setConnectTimeout(5 * 1000);
            conn.setDoOutput(true);//如果通过post提交数据,必须设置允许对外输出数据
            conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            conn.setRequestProperty("Content-Length", String.valueOf(entitydata.length));
            OutputStream outStream = conn.getOutputStream();
            outStream.write(entitydata);
            outStream.flush();
            outStream.close();
            if(conn.getResponseCode()==200){
                return true;
            }
            return false;
        }
  • 相关阅读:
    7-4
    7-3
    第五章例5-2
    第五章例5-1
    第四章例4-12
    第四章例4-11
    第四章例4-10
    第四章例4-9
    第四章例4-8
    第四章例4-7
  • 原文地址:https://www.cnblogs.com/zhuawang/p/12661141.html
Copyright © 2011-2022 走看看