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的请求带参数传递的全部内容,更多相关httpClient内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复