public static String httpPost(String url,Map<String,String> params){
String result = null;
CloseableHttpClient httpClient = HttpClients.createDefault();
//2.声明post请求
HttpPost httpPost = new HttpPost(url);
//3.设置请求类型
httpPost.addHeader("Content-Type","application/x-www-form-urlencoded");
//4.添加参数
List<NameValuePair> parameters = new ArrayList<NameValuePair>();
for (Map.Entry<String, String> entry : params.entrySet()) {
parameters.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
}
UrlEncodedFormEntity formEntity;
try {
formEntity = new UrlEncodedFormEntity(parameters,"UTF-8");
httpPost.setEntity(formEntity);
//5.发送请求
CloseableHttpResponse response = httpClient.execute(httpPost);
LOG.debug("----------------返回信息:"+response.getEntity());
if(response.getStatusLine().getStatusCode()==200){
HttpEntity entity = response.getEntity();
result = EntityUtils.toString(entity, "utf-8");
}
//6.关闭资源
response.close();
httpClient.close();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}
httpClient的post的请求带参数传递
猜你喜欢
转载自blog.csdn.net/weixin_40452506/article/details/106403151
今日推荐
周排行