使用HttpClient发送post请求

/**
 * 
* @Description: 节点生成文书,返回字符串(目前仅有组庭、受理)
* @param url
* @param caseId
* @param jsonData
* @return
* @throws BusinessException
 * @throws IOException 
 */
//@Async
public JsonResult sendPostMessageMap(String url,String caseId,Map jsonMap) throws BusinessException, IOException{
   InputStreamReader inputStreamReader = null;
   try {
      HttpClient client = HttpPoolUtil.getHttpClient();
        HttpPost post = new HttpPost(url);
        //添加请求头
        //post.setHeader("User-Agent", "Mozilla/5.0");
        post.setHeader("User-Agent", "Mozilla/5.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; QQDownload 1.7; .NET CLR 1.1.4322; CIBA; .NET CLR 2.0.50727)");
        List urlParameters = new ArrayList();
        if(!StringUtils.isEmpty(caseId)) {
           urlParameters.add(new BasicNameValuePair("caseId", caseId));
        }

        if(jsonMap!=null)
           urlParameters.add(new BasicNameValuePair("jsonData", JSON.toJSONString(jsonMap)));
        
        
        post.setEntity(new UrlEncodedFormEntity(urlParameters,StandardCharsets.UTF_8));
        log.info("\n Sending 'POST' request to URL : " + url + "Response Code : ");
        HttpResponse response = client.execute(post);
        log.info("\n Sending 'POST' request to URL : " + url + "Response Code : " + response.getStatusLine().getStatusCode());
        //获取数据流
        inputStreamReader = new InputStreamReader(response.getEntity().getContent());
        BufferedReader rd = new BufferedReader(inputStreamReader);
        String abc = rd.readLine();
        JsonResult jsonResult = JSON.parseObject(abc,JsonResult.class);
        if(jsonResult.getFlag() == false) {
           throw new BusinessException(ErrorEnum.ERROR_SYSTEM,jsonResult.getMessage());
        }
        return jsonResult;
   } catch (Exception e) {
      log.info("调用生成文件出错,原因为:"+e.toString());
      throw new BusinessException(ErrorEnum.ERROR_SYSTEM,e.getMessage());
   }finally {
      if(inputStreamReader != null) {
         inputStreamReader.close();
      }
   }
   }

你可能感兴趣的:(使用HttpClient发送post请求)