码迷,mamicode.com
首页 > Web开发 > 详细

发送httpPost请求

时间:2017-10-27 18:06:01      阅读:197      评论:0      收藏:0      [点我收藏+]

标签:中文   获取   entity   utils   exe   ons   finally   utf-8   log   

	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;
		
	}

  

发送httpPost请求

标签:中文   获取   entity   utils   exe   ons   finally   utf-8   log   

原文地址:http://www.cnblogs.com/linhaotown/p/7744079.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!