首页 > Web开发 > 详细

发送httpPost请求

时间:2017-10-27 17:17:59      阅读:331      评论:0      收藏: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;
		
	}

  

发送httpPost请求

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!