xiejun
2023-08-14 885baab441cf03e1d3ea02400f9eee9aa530c7fa
Source/UBCS/ubcs-codeApply/src/main/java/com/vci/ubcs/codeapply/utils/HttpUtil.java
@@ -4,9 +4,11 @@
import com.alibaba.nacos.shaded.com.google.gson.JsonObject;
import com.vci.ubcs.code.vo.webserviceModel.apply.InterParameterVO;
import com.vci.ubcs.codeapply.object.R;
import com.vci.ubcs.codeapply.object.TokenUserObject;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.http.*;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
@@ -16,9 +18,12 @@
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.apache.poi.ss.formula.functions.T;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
@@ -173,7 +178,102 @@
      }
      return result;
   }
   /**
    * 发送HttpPost请求,参数为map
    * @param url
    * @param jsonDataStr
    * @return
    */
   public static R sendPost(String url, String jsonDataStr,Map<String,String> headers) {
      R r=new R();
//        JsonObject formparams = new JsonObject();
//        for (Map.Entry<String, String> entry : map.entrySet()) {
//            formparams.add(entry.getKey(), entry.getValue();
//        }
      //json 格式
        //UrlEncodedFormEntity entity = new UrlEncodedFormEntity(jsonDataStr, Consts.UTF_8);
//        System.out.println(jsonObject.toString());
      StringEntity entity = new StringEntity(jsonDataStr, Consts.UTF_8);
      HttpPost httppost = new HttpPost(url);
      /*
       * 添加请求头信息
       */
      if(headers!=null&&headers.size()>0) {
         for (Map.Entry<String, String> entry : headers.entrySet()) {
            httppost.addHeader(entry.getKey(), entry.getValue());
         }
      }
      httppost.setEntity(entity);
      CloseableHttpResponse response = null;
      try {
         response = httpclient.execute(httppost);
      } catch (IOException e) {
         e.printStackTrace();
      }
      HttpEntity entity1 = response.getEntity();
      String result = null;
      try {
         result = EntityUtils.toString(entity1, "UTF-8");
         // 使用Apache提供的工具类进行转换成字符串
         if(StringUtils.isNotBlank(result)){
            r = JSONObject.toJavaObject(JSONObject.parseObject(result), R.class);
         }
      } catch (ParseException | IOException e) {
         e.printStackTrace();
      }
      return r;
   }
   /**
    * 发送HttpPost请求,参数为map
    * @param url
    * @param map
    * @return
    */
   public static R sendPost(String url, Map<String,String> map,Map<String,String> headers) {
      R r=new R();
//        JsonObject formparams = new JsonObject();
//        for (Map.Entry<String, String> entry : map.entrySet()) {
//            formparams.add(entry.getKey(), entry.getValue();
//        }
      //json 格式
//        UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams, Consts.UTF_8);
      JsonObject jsonObject = new JsonObject();
      for(Map.Entry entry:map.entrySet()){
//            System.out.println(entry.getKey()+ "###########" + entry.getValue());
         jsonObject.addProperty(entry.getKey().toString(),entry.getValue().toString());
      }
//        System.out.println(jsonObject.toString());
      StringEntity entity = new StringEntity(jsonObject.toString(), Consts.UTF_8);
      HttpPost httppost = new HttpPost(url);
      /*
       * 添加请求头信息
       */
      if(headers!=null&&headers.size()>0) {
         for (Map.Entry<String, String> entry : headers.entrySet()) {
            httppost.addHeader(entry.getKey(), entry.getValue());
         }
      }
      httppost.setEntity(entity);
      CloseableHttpResponse response = null;
      try {
         response = httpclient.execute(httppost);
      } catch (IOException e) {
         e.printStackTrace();
      }
      HttpEntity entity1 = response.getEntity();
      String result = null;
      try {
         result = EntityUtils.toString(entity1, "UTF-8");
         // 使用Apache提供的工具类进行转换成字符串
         if(StringUtils.isNotBlank(result)){
            r = JSONObject.toJavaObject(JSONObject.parseObject(result), R.class);
         }
      } catch (ParseException | IOException e) {
         e.printStackTrace();
      }
      return r;
   }
   /**
    * 发送HttpPost请求,参数为map
    * @param url
@@ -211,7 +311,49 @@
      }
      return result;
   }
   /**
    * 发送HttpPost请求,参数为map
    * @param url
    * @param dataMap
    * @return
    */
   public static String sendFormPost(String url, Map<String,String> dataMap, Map<String,String> headers) {
      String result = null;
      try {
         //json 格式
         List<NameValuePair> nvps = new ArrayList<>();
//
         HttpPost httppost = new HttpPost(url);
         if (dataMap != null && dataMap.size() > 0) {
            for (Map.Entry<String, String> entry : dataMap.entrySet()) {
               nvps.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
            }
         }
         /*
          * 添加请求头信息
          */
         if (headers != null && headers.size() > 0) {
            for (Map.Entry<String, String> entry : headers.entrySet()) {
               httppost.addHeader(entry.getKey(), entry.getValue());
            }
         }
         httppost.setEntity(new UrlEncodedFormEntity(nvps, "utf-8")); //将参数传入post方法中
         //httppost.setEntity(entity);
         CloseableHttpResponse response = null;
         response = httpclient.execute(httppost);
         HttpEntity entity1 = response.getEntity();
         result = EntityUtils.toString(entity1, "UTF-8");
         // 使用Apache提供的工具类进行转换成字符串
         if (StringUtils.isNotBlank(result)) {
            return result;
         }
      }catch (Throwable e){
         e.printStackTrace();;
      }
      return result;
   }
   /**
    * 发送不带参数的HttpPost请求
    * @param url