派生自 projectDept/qhighschool

EricsHu
2022-12-05 068fc7f2e81178e55fa191a13709af64b1a163f6
src/main/java/com/qxueyou/scc/base/util/HttpClient.java
@@ -75,7 +75,7 @@
   }
   
   private void init() {
      X509TrustManager xtm = new X509TrustManager() { // 创建TrustManager
      X509TrustManager xtm = new X509TrustManager() { // 创建TrustManager
         public void checkClientTrusted(X509Certificate[] chain,
               String authType) throws CertificateException {
         }
@@ -89,9 +89,9 @@
         }
      };
      try {
         // TLS1.0与SSL3.0基本上没有太大的差别,可粗略理解为TLS是SSL的继承者,但它们使用的是相同的SSLContext
         // TLS1.0与SSL3.0基本上没有太大的差别,可粗略理解为TLS是SSL的继承者,但它们使用的是相同的SSLContext
         SSLContext ctx = SSLContext.getInstance("TLS");
         // 使用TrustManager来初始化该上下文,TrustManager只是被SSL的Socket所使用
         // 使用TrustManager来初始化该上下文,TrustManager只是被SSL的Socket所使用
         ctx.init(null, new TrustManager[] { xtm }, null);
         /*
@@ -145,13 +145,13 @@
   }
   /**
    * 向HTTPS地址发送POST请求
    * 向HTTPS地址发送POST请求
    * 
    * @param reqURL
    *            请求地址
    *            请求地址
    * @param params
    *            请求参数
    * @return 响应内容
    *            请求参数
    * @return 响应内容
    */
   @SuppressWarnings("unchecked")
   public String sendStringPostRequest(String reqURL, Map<String, String> params1) {
@@ -161,7 +161,7 @@
            params = Collections.EMPTY_MAP;
         }
         HttpEntity entity = sendPostRequest(reqURL, params); // 获取响应实体
         HttpEntity entity = sendPostRequest(reqURL, params); // 获取响应实体
         
         if( null != entity ){
            String content = EntityUtils.toString(entity, UTF_8);
@@ -184,13 +184,13 @@
   }
   /**
    * 向HTTPS地址发送POST请求
    * 向HTTPS地址发送POST请求
    * 
    * @param reqURL
    *            请求地址
    *            请求地址
    * @param params
    *            请求参数
    * @return 响应内容
    *            请求参数
    * @return 响应内容
    */
   @SuppressWarnings("unchecked")
   public String sendStringPostRequest(String reqURL, String data) {
@@ -198,7 +198,7 @@
         StringEntity reqEntity = new StringEntity(data, UTF_8);
         HttpEntity entity = sendPostRequest(reqURL, Collections.EMPTY_MAP, reqEntity); // 获取响应实体
         HttpEntity entity = sendPostRequest(reqURL, Collections.EMPTY_MAP, reqEntity); // 获取响应实体
         if( null != entity ){
            
@@ -223,13 +223,13 @@
   }
   /**
    * 向HTTPS地址发送POST请求
    * 向HTTPS地址发送POST请求
    * 
    * @param reqURL
    *            请求地址
    *            请求地址
    * @param params
    *            请求参数
    * @return 响应内容
    *            请求参数
    * @return 响应内容
    */
   public String sendStringPostRequest(String reqURL, Map<String, String> headerParas1, String data) {
      Map<String, String> headerParas = headerParas1;
@@ -237,7 +237,7 @@
         StringEntity reqEntity = new StringEntity(data, UTF_8);
         HttpEntity entity = sendPostRequest(reqURL, headerParas, reqEntity); // 获取响应实体
         HttpEntity entity = sendPostRequest(reqURL, headerParas, reqEntity); // 获取响应实体
         
         if( null != entity ){
            String content = EntityUtils.toString(entity, UTF_8);
@@ -260,13 +260,13 @@
   }
   /**
    * 向HTTPS地址发送POST请求
    * 向HTTPS地址发送POST请求
    * 
    * @param reqURL
    *            请求地址
    *            请求地址
    * @param params
    *            请求参数
    * @return 响应内容
    *            请求参数
    * @return 响应内容
    */
   @SuppressWarnings("unchecked")
   public HttpEntity sendPostRequest(String reqURL, Map<String, String> params1) {
@@ -319,7 +319,7 @@
      Map<String, String> headerParas = headerParas1;
      try {
         HttpPost httpPost = new HttpPost(reqURL); // 创建HttpPost
         HttpPost httpPost = new HttpPost(reqURL); // 创建HttpPost
         httpPost.setEntity(reqEntity);
         headerParas = checkUserAgent(headerParas);
@@ -332,18 +332,18 @@
            log.debug("request--header:" + header.getName() + ":" + header.getValue());
         }
         HttpResponse response = httpClient.execute(httpPost); // 执行POST请求
         HttpResponse response = httpClient.execute(httpPost); // 执行POST请求
         headers = response.getAllHeaders();
         for (Header header : headers) {
            log.debug("response--header:" + header.getName() + ":" + header.getValue());
         }
         HttpEntity entity = response.getEntity(); // 获取响应实体
         HttpEntity entity = response.getEntity(); // 获取响应实体
         this.debug(httpPost.getURI(), response.getStatusLine().getStatusCode(), entity.getContentLength(), entity.getContentType());
         if (302 == response.getStatusLine().getStatusCode() && response.containsHeader("location")) {
            String location = response.getLastHeader("location").getValue();
            log.debug("重定向:" + location);
            log.debug("重定向:" + location);
            EntityUtils.consume(entity);
            return sendPostRequest(location, headerParas, reqEntity);
         }
@@ -366,7 +366,7 @@
      Map<String, String> headerParas = headerParas1;
      try {
         HttpDelete httpDelete = new HttpDelete(reqURL); // 创建HttpPost
         HttpDelete httpDelete = new HttpDelete(reqURL); // 创建HttpPost
         headerParas = checkUserAgent(headerParas);
         for (String key : headerParas.keySet()) {
@@ -378,12 +378,12 @@
            log.debug("request--header:" + header.getName() + ":" + header.getValue());
         }
         HttpResponse response = httpClient.execute(httpDelete); // 执行POST请求
         HttpResponse response = httpClient.execute(httpDelete); // 执行POST请求
         headers = response.getAllHeaders();
         for (Header header : headers) {
            log.debug("response--header:" + header.getName() + ":" + header.getValue());
         }
         HttpEntity entity = response.getEntity(); // 获取响应实体
         HttpEntity entity = response.getEntity(); // 获取响应实体
         this.debug(httpDelete.getURI(), response.getStatusLine().getStatusCode(), entity.getContentLength(), entity.getContentType());
@@ -414,17 +414,17 @@
    * @param header
    */
   private void debug(URI uri, int code, long length, Header header){
      log.debug("请求地址: " + uri);
      log.debug("响应状态: " + code);
      log.debug("响应长度: " + length);
      log.debug("响应类型: " + header);
      log.debug("请求地址: " + uri);
      log.debug("响应状态: " + code);
      log.debug("响应长度: " + length);
      log.debug("响应类型: " + header);
   }
   public String sendStringPutRequest(String reqURL, Map<String, String> headerParas1, String body) {
      Map<String, String> headerParas = headerParas1;
      try {
         HttpPut httpPut = new HttpPut(reqURL); // 创建HttpPost
         HttpPut httpPut = new HttpPut(reqURL); // 创建HttpPost
         headerParas = checkUserAgent(headerParas);
         for (String key : headerParas.keySet()) {
@@ -439,12 +439,12 @@
         StringEntity reqEntity = new StringEntity(body, UTF_8);
         httpPut.setEntity(reqEntity);
         HttpResponse response = httpClient.execute(httpPut); // 执行POST请求
         HttpResponse response = httpClient.execute(httpPut); // 执行POST请求
         headers = response.getAllHeaders();
         for (Header header : headers) {
            log.debug("response--header:" + header.getName() + ":" + header.getValue());
         }
         HttpEntity entity = response.getEntity(); // 获取响应实体
         HttpEntity entity = response.getEntity(); // 获取响应实体
         this.debug(httpPut.getURI(), response.getStatusLine().getStatusCode(), entity.getContentLength(), entity.getContentType());
         
@@ -470,7 +470,7 @@
      Map<String, String> headerParas = headerParas1;
      try {
         HttpPost httpPost = new HttpPost(reqURL); // 创建HttpPost
         HttpPost httpPost = new HttpPost(reqURL); // 创建HttpPost
         BasicHttpEntity requestBody = new BasicHttpEntity();
         requestBody.setContent(new ByteArrayInputStream(param.getBytes(UTF_8)));
@@ -490,18 +490,18 @@
         log.debug("request--param:" + param);
         HttpResponse response = httpClient.execute(httpPost); // 执行POST请求
         HttpResponse response = httpClient.execute(httpPost); // 执行POST请求
         headers = response.getAllHeaders();
         for (Header header : headers) {
            log.debug("response--header:" + header.getName() + ":" + header.getValue());
         }
         HttpEntity entity = response.getEntity(); // 获取响应实体
         HttpEntity entity = response.getEntity(); // 获取响应实体
         this.debug(httpPost.getURI(), response.getStatusLine().getStatusCode(), entity.getContentLength(), entity.getContentType());
         
         if (302 == response.getStatusLine().getStatusCode() && response.containsHeader("location")) {
            String location = response.getLastHeader("location").getValue();
            log.debug("重定向:" + location);
            log.debug("重定向:" + location);
            EntityUtils.consume(entity);
            return sendPostRequest(location, headerParas, param);
         }
@@ -566,7 +566,7 @@
      Map<String, String> params = params1;
      Map<String, String> headerParas = headerParas1;
      try {
         // HttpGet httpGet = new HttpGet(reqURL); // 创建HttpPost
         // HttpGet httpGet = new HttpGet(reqURL); // 创建HttpPost
         headerParas = checkUserAgent(headerParas);
@@ -590,19 +590,19 @@
            log.debug("request--header:" + header.getName() + ":" + header.getValue());
         }
         // HttpResponse response = httpClient.execute(httpGet); // 执行POST请求
         // HttpResponse response = httpClient.execute(httpGet); // 执行POST请求
         HttpResponse response = httpClient.execute(request);
         headers = response.getAllHeaders();
         for (Header header : headers) {
            log.debug("response--header:" + header.getName() + ":" + header.getValue());
         }
         HttpEntity entity = response.getEntity(); // 获取响应实体
         HttpEntity entity = response.getEntity(); // 获取响应实体
         this.debug(request.getURI(), response.getStatusLine().getStatusCode(), entity.getContentLength(), entity.getContentType());
         
         if (302 == response.getStatusLine().getStatusCode() && response.containsHeader("location")) {
            String location = response.getLastHeader("location").getValue();
            log.debug("重定向");
            log.debug("重定向");
            return sendGetRequestAsResponse(location, headerParas, params);
         }