使用HttpClient發送post請求

/**
 * 
* @Description: 節點生成文書,返回字符串(目前僅有組庭、受理)
* @param url
* @param caseId
* @param jsonData
* @return
* @throws BusinessException
 * @throws IOException 
 */
//@Async
public JsonResult<String> 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<NameValuePair> urlParameters = new ArrayList<NameValuePair>();
        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<String> 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();
      }
   }
   }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章