南昌市物联网技防平台-后台
zengh
2021-06-01 cd2cfd37366002f790a2ed1435962d4eff7d5cec
blade-service/blade-jfpts/src/main/java/org/springblade/jfpt/parcel/util/HttpClientUtils.java
@@ -35,6 +35,7 @@
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.ssl.SSLContextBuilder;
import org.apache.http.util.EntityUtils;
import sun.misc.BASE64Encoder;
import javax.net.ssl.SSLContext;
@@ -67,16 +68,12 @@
            HttpGet get = new HttpGet(builder.build());
            response = httpclient.execute(get);
            System.out.println(response.getStatusLine());
            if (200 == response.getStatusLine().getStatusCode()) {
                HttpEntity entity = response.getEntity();
                System.out.println(entity.toString());
                resultString = EntityUtils.toString(entity, "utf-8");
                System.out.println(resultString.toString());
            }
        } catch (Exception e) {
@@ -101,6 +98,125 @@
        return resultString;
    }
   /**
    * 执行有参GET请求,带请求头
    *
    * @param url 请求url
    * @param params 参数
    * @param key 请求头Key
    * @param secretKey 秘钥
    * @return
    */
   public static String doGetHeader(String url, String key, String secretKey,Map<String, String> params) {
      //获取httpclient客户端
      CloseableHttpClient httpclient = HttpClients.createDefault();
      String resultString = "";
      CloseableHttpResponse response = null;
      try {
         URIBuilder builder = new URIBuilder(url);
         if (null != params) {
            for (String keys : params.keySet()) {
               builder.addParameter(keys,params.get(keys));
               //builder.setParameter(keys, params.get(keys));
            }
         }
         HttpGet httpGet = new HttpGet(builder.build());
         //设置请求头
         httpGet.addHeader(key,secretKey);
         // 传输的类型
         httpGet.addHeader("Content-Type", "application/x-www-form-urlencoded");
         //执行Http请求调用
         response = httpclient.execute(httpGet);
         //判断是否请求成功返回
         if (200 == response.getStatusLine().getStatusCode()) {
            HttpEntity entity = response.getEntity();
            resultString = EntityUtils.toString(entity, "utf-8");
         }
      } catch (Exception e) {
         e.printStackTrace();
      } finally {
         if (null != response) {
            try {
               response.close();
            } catch (IOException e) {
               e.printStackTrace();
            }
         }
         if (null != httpclient) {
            try {
               httpclient.close();
            } catch (IOException e) {
               e.printStackTrace();
            }
         }
      }
      return resultString;
   }
   /**
    * 执行有参GET请求,带请求头,接收图片流
    *
    * @param url 请求url
    * @param params 参数
    * @param key 请求头Key
    * @param secretKey 秘钥
    * @return
    */
   public static String doGetHeaderPictureBase64(String url, String key, String secretKey,Map<String, String> params) {
      //获取httpclient客户端
      CloseableHttpClient httpclient = HttpClients.createDefault();
      String resultString = "";
      CloseableHttpResponse response = null;
      try {
         URIBuilder builder = new URIBuilder(url);
         if (null != params) {
            for (String keys : params.keySet()) {
               builder.addParameter(keys,params.get(keys));
            }
         }
         HttpGet httpGet = new HttpGet(builder.build());
         //设置请求头
         httpGet.addHeader(key,secretKey);
         // 传输的类型
         httpGet.addHeader("Content-Type", "application/x-www-form-urlencoded");
         //执行Http请求调用
         response = httpclient.execute(httpGet);
         // 将返回的图片或者文件转化成字节数组的形式
         byte[] data = EntityUtils.toByteArray(response.getEntity());
         BASE64Encoder encoder = new BASE64Encoder();
         //String imageBase64 = "data:image/png;base64," + encoder.encodeBuffer(data).trim();
         return encoder.encodeBuffer(data).trim().replaceAll("\n", "").replaceAll("\r", "").replaceAll(" ", "");//删除 \r\n
      } catch (Exception e) {
         e.printStackTrace();
      } finally {
         if (null != response) {
            try {
               response.close();
            } catch (IOException e) {
               e.printStackTrace();
            }
         }
         if (null != httpclient) {
            try {
               httpclient.close();
            } catch (IOException e) {
               e.printStackTrace();
            }
         }
      }
      return resultString;
   }
    /**
     * 执行无参GET请求
@@ -160,8 +276,6 @@
            response = httpclient.execute(post);
            System.out.println(response.getStatusLine());
            if (200 == response.getStatusLine().getStatusCode()) {
                HttpEntity entity = response.getEntity();
                resultString = EntityUtils.toString(entity, "utf-8");
@@ -199,8 +313,6 @@
        Map<String, String> params = new HashMap<>();
        params.put("scope", "project");
        params.put("q", "数据库");
//
//      System.out.println(doGet("http://www.baidu.com/s",params));
        /**
         * 有一部分网站,禁止爬虫技术访问网站。
@@ -220,7 +332,7 @@
    * @param map
    * @return
    */
    public static String httpPost(String url, String appKey, String appKeyValue, Map<String, String> map) {
    public static String httpPost(String url, String appKey, String appKeyValue, Map<String, Object> map) {
        // 返回body
        String body = null;
        // 获取连接客户端工具
@@ -364,9 +476,7 @@
                    param.append(entry.getKey());
                    param.append("=");
                    param.append(entry.getValue());
                    // System.out.println(entry.getKey()+":"+entry.getValue());
                }
                // System.out.println("param:"+param.toString());
                out.write(param.toString());
            }
            // flush输出流的缓冲