public static String sendHttpPost(String url, Map<String, String> 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<String, String> 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;
}
发送Post请求application/x-www-form-urlencoded
最新推荐文章于 2024-09-09 18:47:40 发布