http post body 参数map

	/**
	 * 作者:崔金龙
	 * @param urlPath
	 * @param map
	 * @return
	 * @throws Exception
	 */
	public static String postBody(String urlPath, Map<String, Object> map) throws Exception {
		// 定义一个可关闭的httpClient的对象
		CloseableHttpClient httpClient = null;
		// 定义httpPost对象
		HttpPost post = null;
		// 返回结果
		String result = null;
		try {
			// 1.创建httpClient的默认实例
			httpClient = HttpClients.createDefault();
			// 2.提交post
			post = new HttpPost(urlPath);
			if(map!=null&&map.size()>0){
                List<NameValuePair> list = new ArrayList<NameValuePair>();
                for(Map.Entry<String,Object> entry:map.entrySet()){
                    list.add(new BasicNameValuePair(entry.getKey(),URLEncoder.encode(entry.getValue().toString(), "utf-8")));
                }
                UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list,"utf-8");
                post.setEntity(entity);
            }
			CloseableHttpResponse response = httpClient.execute(post);
			try {
				if (response != null) {
					 HttpEntity httpEntity = response.getEntity();
					// 如果返回的内容不为空
					 if (httpEntity != null) {
						 result = EntityUtils.toString(httpEntity);
					 }
				}
			}catch (Exception e) {
				e.printStackTrace();
			}finally {
				//关闭response
				 response.close();
			}
		}catch (Exception e) {
			        e.printStackTrace();
        }finally{
        		try {
        			//关闭资源
        			httpClient.close();
        		}catch (IOException e) {
        			e.printStackTrace();
        		}
        }
		return result;

	}
https://www.cnblogs.com/xglbk/p/6207098.html

 

转载于:https://www.cnblogs.com/cuijinlong/p/8400323.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值