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