我是靠谱客的博主 无私溪流,最近开发中收集的这篇文章主要介绍httpClient的post的请求带参数传递,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

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的post的请求带参数传递所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(82)

评论列表共有 0 条评论

立即
投稿
返回
顶部