Post方式发送http请求参数

xiaoxiao2021-03-01  11

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; }
转载请注明原文地址: https://www.6miu.com/read-4303031.html

最新回复(0)