发送Post请求application/x-www-form-urlencoded

public static String sendHttpPost(String url, Map map, String encoding) throws Exception {
String body = “”;
//创建httpClient对象
CloseableHttpClient client = HttpClients.createDefault();
//创建post方式请求对象
HttpPost httpPost = new HttpPost(url);
List nvps = new ArrayList<>();
if (map != null) {
for (Map.Entry enrtry : map.entrySet()) {
nvps.add(new BasicNameValuePair(enrtry.getKey(), enrtry.getValue()));
}
}
httpPost.setEntity(new UrlEncodedFormEntity(nvps, encoding));
httpPost.setHeader(“Content-type”, “application/x-www-form-urlencoded”);
CloseableHttpResponse response = client.execute(httpPost);
HttpEntity entity = response.getEntity();
if (entity != null) {
body = EntityUtils.toString(entity, encoding);
}
EntityUtils.consume(entity);
response.close();
return body;
}

你可能感兴趣的:(java,eclipse,java,intellij-idea)