zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    MongoDB学习笔记(二)
    mongoDB学习笔记(一)
    docker官方文档笔记
    nagios
    网络流量状态命令总结 (含notp安装)
    other
    一键搭建LNMP脚本
    linux问题总结
    linux中VI编写C程序。。。
    centos 7 安装python3.5.1
  • 原文地址:https://www.cnblogs.com/cuijinlong/p/8400323.html
Copyright © 2011-2022 走看看