发送httpPost请求

Posted on 2017-10-27 16:41  林浩开发小屋  阅读(263)  评论(0)    收藏  举报
	public static JSONObject sendPost(JSONObject jsonParam, String url)  {
		LOGGER.info("获取回执信息请求参数:"+jsonParam.toString());
		JSONObject resultJson = null;
				
		//创建httpclient对象  
        CloseableHttpClient client = HttpClients.createDefault();

        //创建post方式请求对象  
        HttpPost httpPost = new HttpPost(url);
        
        StringEntity entity=null;
        if(jsonParam != null) {
        	entity = new StringEntity(jsonParam.toString(),"utf-8");// 解决中文乱码问题  
        }        
        entity.setContentEncoding("UTF-8");      
        entity.setContentType("application/json");      
        httpPost.setEntity(entity);  
              
        // 发起请求  
        HttpResponse httpResponse = null;
        String resData = null;
		try {
			httpResponse = client.execute(httpPost);
			resData = EntityUtils.toString(httpResponse.getEntity(), Charsets.UTF_8.name());
		} catch (IOException e) {
			LOGGER.error(e.getMessage(), e);
		} finally {
			try {
				client.close();
			} catch (IOException e) {
				LOGGER.error(e.getMessage(), e);
			}
		}		
        resultJson = JSON.parseObject(resData);
		return resultJson;
		
	}

  

博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3