private static String getContent(String url, Map<String, String> mapdata) {
CloseableHttpResponse response = null;
CloseableHttpClient httpClient = HttpClients.createDefault();
// 创建httppost
HttpPost httpPost = new HttpPost(url);
try {
// 设置提交方式
httpPost.addHeader("Content-type", "application/x-www-form-urlencoded; charset=utf-8");
// 添加参数
List<NameValuePair> nameValuePairs = new ArrayList<>();
if (mapdata.size() != 0) {
// 将mapdata中的key存在set集合中,通过迭代器取出所有的key,再获取每一个键对应的值
Set keySet = mapdata.keySet();
Iterator it = keySet.iterator();
while (it.hasNext()) {
String k = it.next().toString();// key
String v = mapdata.get(k);// value
nameValuePairs.add(new BasicNameValuePair(k, v));
}
}
httpPost.setEntity( new UrlEncodedFormEntity(nameValuePairs,"UTF-8"));
// 执行http请求
response = httpClient.execute(httpPost);
// 获得http响应体
org.apache.http.HttpEntity entity = response.getEntity();
if (entity != null) {
// 响应的结果
String content = EntityUtils.toString(entity, "UTF-8");
return content;
}
} catch (Exception e) {
e.printStackTrace();
}
return "获取数据错误";
}
JAVA调用HttpClient发送POST请求
最新推荐文章于 2024-05-11 13:02:29 发布