智慧保安后台管理-外网项目备份
zhongrj
2023-09-17 8853292babb2ad94de4a3207966f1e83b767cd2d
src/main/java/org/springblade/common/utils/HttpReqUtil.java
@@ -107,6 +107,21 @@
      return postJson(url, json, header);
   }
   public String doPostJsons(String url, Map<String, Object> params, Map<String, String> header) throws Exception {
      String json = null;
      if (params != null && !params.isEmpty()) {
         for (Iterator<Entry<String, Object>> it = params.entrySet().iterator(); it.hasNext();) {
            Entry<String, Object> entry = (Entry<String, Object>) it.next();
            Object object = entry.getValue();
            if (object == null) {
               it.remove();
            }
         }
         json = JSON.toJSONString(params);
      }
      return postJsons(url, json, header);
   }
   public String doPostJson(String url, String json) throws Exception {
      return doPostJson(url, json, null);
   }
@@ -144,6 +159,35 @@
      return body;
   }
   private String postJsons(String url, String json, Map<String, String> header) throws Exception {
      String body = null;
      try {
         // Post请求
         LOG.debug(" protocol: POST");
         LOG.debug("      url: " + url);
         HttpPost httpPost = new HttpPost(url.trim());
         // 设置参数
         LOG.debug("   params: " + json);
         httpPost.setEntity(new StringEntity(json, ContentType.DEFAULT_TEXT.withCharset(charset)));
         httpPost.setHeader(new BasicHeader("Content-Type", "application/json"));
         LOG.debug("     type: JSON");
         // 设置Header
         if (header != null && !header.isEmpty()) {
            LOG.debug("   header: " + JSON.toJSONString(header));
            for (Iterator<Entry<String, String>> it = header.entrySet().iterator(); it.hasNext();) {
               Entry<String, String> entry = (Entry<String, String>) it.next();
               httpPost.setHeader(new BasicHeader(entry.getKey(), entry.getValue()));
            }
         }
         // 发送请求,获取返回数据
         body = execute(httpPost);
      } catch (Exception e) {
         throw e;
      }
      LOG.debug("  result: " + body);
      return body;
   }
   /**
    * get请求
    */