public boolean uploadPostMethod(String path, Map<String, String> params) throws IOException{ StringBuilder sb = new StringBuilder(); for(Map.Entry<String, String> entry : params.entrySet()){ //sb.append(entry.getKey()).append('=').append(entry.getValue()).append('&'); sb.append(entry.getKey()).append('=').append(URLEncoder.encode(entry.getValue(), "UTF-8")).append('&'); } sb.deleteCharAt(sb.length() - 1); byte[] entitydata = sb.toString().getBytes(); URL url = new URL(path); HttpURLConnection conn = (HttpURLConnection)url.openConnection(); conn.setConnectTimeout(5 * 1000); conn.setRequestMethod("POST"); conn.setDoOutput(true); conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); conn.setRequestProperty("Content-Length", String.valueOf(entitydata.length)); OutputStream os = conn.getOutputStream(); os.write(entitydata); os.flush(); os.close(); if(conn.getResponseCode() == 200){ return true; } return false; }
Post方式发送http请求参数
最新推荐文章于 2024-08-04 21:17:00 发布