From eb6b6dbb35a9f029e0b7d269773685c19fd40976 Mon Sep 17 00:00:00 2001
From: 无关风月 <443237572@qq.com>
Date: 星期四, 11 七月 2024 10:47:51 +0800
Subject: [PATCH] 玩湃微信商户认证代码

---
 cloud-server-other/src/main/java/com/dsh/other/util/HttpClientUtil.java |  121 ++++++++++++++++++++-------------------
 1 files changed, 62 insertions(+), 59 deletions(-)

diff --git a/cloud-server-other/src/main/java/com/dsh/other/util/HttpClientUtil.java b/cloud-server-other/src/main/java/com/dsh/other/util/HttpClientUtil.java
index 4d493ae..b33680b 100644
--- a/cloud-server-other/src/main/java/com/dsh/other/util/HttpClientUtil.java
+++ b/cloud-server-other/src/main/java/com/dsh/other/util/HttpClientUtil.java
@@ -39,10 +39,10 @@
 
     private static Logger logger = LoggerFactory.getLogger(HttpClientUtil.class);
 
-    private PoolingHttpClientConnectionManager connectionManager;
+    private static PoolingHttpClientConnectionManager connectionManager;
 
 
-    public HttpClientUtil(){
+    public HttpClientUtil() {
         //1.创建连接池管理器
         connectionManager = new PoolingHttpClientConnectionManager(60000,
                 TimeUnit.MILLISECONDS);
@@ -53,14 +53,14 @@
     /**
      * 创建一个httpClient对象
      */
-    private CloseableHttpClient getHttpCline(){
-        return  HttpClients.custom()
+    private static CloseableHttpClient getHttpCline() {
+        return HttpClients.custom()
                 .setConnectionManager(connectionManager)
                 .disableAutomaticRetries()
                 .build();
     }
 
-    private RequestConfig getRequestConfig(){
+    private static RequestConfig getRequestConfig() {
         RequestConfig.Builder builder = RequestConfig.custom();
         builder.setSocketTimeout(60000)//3.1设置客户端等待服务端返回数据的超时时间
                 .setConnectTimeout(30000)//3.2设置客户端发起TCP连接请求的超时时间
@@ -70,34 +70,34 @@
     }
 
 
-
     /**
      * 创建一个POST请求实例
-     * @param url       请求地址
-     * @param params    请求参数
+     *
+     * @param url    请求地址
+     * @param params 请求参数
      */
-    private CloseableHttpResponse setPostHttpRequset(String url, Map<String, Object> params, Map<String, String> header, String contentType) throws Exception{
+    private CloseableHttpResponse setPostHttpRequset(String url, Map<String, Object> params, Map<String, String> header, String contentType) throws Exception {
         HttpPost httpPost = new HttpPost(url);
         httpPost.setConfig(this.getRequestConfig());
-        if(null != header){
-            for(String key : header.keySet()){
+        if (null != header) {
+            for (String key : header.keySet()) {
                 httpPost.setHeader(key, header.get(key));
             }
         }
         List<NameValuePair> list = new ArrayList<>();
-        if(null != params){
+        if (null != params) {
             Set<String> keys = params.keySet();
-            for(String key : keys){
+            for (String key : keys) {
                 list.add(new BasicNameValuePair(key, null == params.get(key) ? null : params.get(key).toString()));
             }
         }
-        switch (contentType){
+        switch (contentType) {
             case "form":
                 httpPost.setEntity(new UrlEncodedFormEntity(list, "UTF-8"));
                 break;
             case "json":
                 ObjectMapper objectMapper = new ObjectMapper();
-                String s =objectMapper.writeValueAsString(params);
+                String s = objectMapper.writeValueAsString(params);
                 httpPost.setEntity(new StringEntity(s, ContentType.create(ContentType.APPLICATION_JSON.getMimeType(), Charset.forName("UTF-8"))));
                 break;
         }
@@ -107,23 +107,24 @@
 
     /**
      * 获取get请求实例
-     * @param url       请求地址
-     * @param params    请求参数
+     *
+     * @param url    请求地址
+     * @param params 请求参数
      */
-    private CloseableHttpResponse setGetHttpRequset(String url, Map<String, Object> params, Map<String, String> header) throws Exception{
+    private CloseableHttpResponse setGetHttpRequset(String url, Map<String, Object> params, Map<String, String> header) throws Exception {
         StringBuffer sb = new StringBuffer();
         String p = "";
-        if(null != params){
+        if (null != params) {
             Set<String> keys = params.keySet();
-            for(String key : keys){
+            for (String key : keys) {
                 sb.append(key + "=" + params.get(key) + "&");
             }
             p = "?" + sb.substring(0, sb.length() - 1);
         }
         HttpGet httpGet = new HttpGet(url + p);
         httpGet.setConfig(getRequestConfig());
-        if(null != header){
-            for(String key : header.keySet()){
+        if (null != header) {
+            for (String key : header.keySet()) {
                 httpGet.setHeader(key, header.get(key));
             }
         }
@@ -133,19 +134,20 @@
 
     /**
      * 发送http请求
-     * @param mothed        "GET、POST、PUT、HEAD、DELETE、HEAD、OPTIONS"
-     * @param url           请求地址
-     * @param params        请求参数
-     * @param header        请求头
-     * @param contentType   参数请求方式form/json
+     *
+     * @param mothed      "GET、POST、PUT、HEAD、DELETE、HEAD、OPTIONS"
+     * @param url         请求地址
+     * @param params      请求参数
+     * @param header      请求头
+     * @param contentType 参数请求方式form/json
      * @return
      */
-    public HttpResult pushHttpRequset(String mothed, String url, Map<String, Object> params, Map<String, String> header, String contentType) throws Exception{
+    public HttpResult pushHttpRequset(String mothed, String url, Map<String, Object> params, Map<String, String> header, String contentType) throws Exception {
         String randome = UUID.randomUUID().toString();
         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");
         logger.info(sdf.format(new Date()) + "----(" + randome + ")请求参数:" + JSON.toJSONString(params));
         CloseableHttpResponse httpResponse = null;
-        switch (mothed){
+        switch (mothed) {
             case "GET":
                 httpResponse = this.setGetHttpRequset(url, params, header);
                 break;
@@ -164,15 +166,16 @@
 
     /**
      * 发送XML请求
-     * @param url       请求地址
-     * @param xml       XML数据
-     * @param header    自定义请求头
+     *
+     * @param url    请求地址
+     * @param xml    XML数据
+     * @param header 自定义请求头
      * @return
      */
-    public HttpResult pushHttpRequsetXml(String url, String xml, Map<String, String> header) throws Exception{
+    public static HttpResult pushHttpRequsetXml(String url, String xml, Map<String, String> header) throws Exception {
         HttpPost httpPost = new HttpPost(url);
         httpPost.setConfig(getRequestConfig());
-        for(String key : header.keySet()){
+        for (String key : header.keySet()) {
             httpPost.setHeader(key, header.get(key));
         }
         httpPost.setHeader("Content-Type", "application/xml");
@@ -181,39 +184,39 @@
         int statusCode = httpResponse.getStatusLine().getStatusCode();
         String content = EntityUtils.toString(httpResponse.getEntity(), "UTF-8");
         HttpResult httpResult = HttpResult.getHttpResult(statusCode, content);
-        this.close(httpResponse);
+        close(httpResponse);
         return httpResult;
     }
 
 
-
     /**
      * 请求https发送XML请求
-     * @param url           接口路径
-     * @param xml           内容
-     * @param header        请求头
-     * @param certPassword      证书密码
-     * @param certPath      证书路径
-     * @param certType      证书类型
+     *
+     * @param url          接口路径
+     * @param xml          内容
+     * @param header       请求头
+     * @param certPassword 证书密码
+     * @param certPath     证书路径
+     * @param certType     证书类型
      * @return
      * @throws Exception
      */
-    public String pushHttpsRequsetXml(String url, String xml, Map<String, String> header, String certPassword, String certPath, String certType) throws Exception{
+    public static String pushHttpsRequsetXml(String url, String xml, Map<String, String> header, String certPassword, String certPath, String certType) throws Exception {
         HttpPost httpPost = new HttpPost(url);
-        for(String key : header.keySet()){
+        for (String key : header.keySet()) {
             httpPost.setHeader(key, header.get(key));
         }
         httpPost.setHeader("Content-Type", "application/xml");
         httpPost.setEntity(new StringEntity(xml, "UTF-8"));
-        CloseableHttpClient httpCline = this.initCert(certPassword, certPath, certType);
+        CloseableHttpClient httpCline = initCert(certPassword, certPath, certType);
         CloseableHttpResponse httpResponse = httpCline.execute(httpPost);
         String content = null;
-        if(httpResponse.getStatusLine().getStatusCode() == 200){
+        if (httpResponse.getStatusLine().getStatusCode() == 200) {
             content = EntityUtils.toString(httpResponse.getEntity(), "UTF-8");
-        }else{
+        } else {
             content = "返回状态码:" + httpResponse.getStatusLine() + "。" + EntityUtils.toString(httpResponse.getEntity());
         }
-        this.close(httpResponse);
+        close(httpResponse);
         httpCline.close();
         return content;
     }
@@ -221,12 +224,13 @@
 
     /**
      * 初始化https对象(带证书)
-     * @param key       证书密码
-     * @param certPath  证书路径
-     * @param certType  证书类型
+     *
+     * @param key      证书密码
+     * @param certPath 证书路径
+     * @param certType 证书类型
      * @throws Exception
      */
-    private CloseableHttpClient initCert(String key, String certPath, String certType) throws Exception {
+    private static CloseableHttpClient initCert(String key, String certPath, String certType) throws Exception {
         KeyStore keyStore = KeyStore.getInstance(certType);
         InputStream inputStream = new FileInputStream(new File(certPath));
         try {
@@ -236,30 +240,29 @@
         }
         SSLContext sslcontext = SSLContexts.custom().loadKeyMaterial(keyStore, key.toCharArray()).build();
         SSLConnectionSocketFactory sslsf =
-                new SSLConnectionSocketFactory(sslcontext, new String[] {"TLSv1"}, null,
+                new SSLConnectionSocketFactory(sslcontext, new String[]{"TLSv1"}, null,
                         SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
         return HttpClients.custom().setSSLSocketFactory(sslsf).build();
     }
 
 
-
     /**
      * 关闭资源
      */
-    private void close(CloseableHttpResponse httpResponse){
+    private static void close(CloseableHttpResponse httpResponse) {
         try {
-            if(null != httpResponse){
+            if (null != httpResponse) {
                 EntityUtils.consume(httpResponse.getEntity());//此处高能,通过源码分析,由EntityUtils是否回收HttpEntity
                 httpResponse.close();
             }
         } catch (Exception e) {
             e.printStackTrace();
-        }finally {
+        } finally {
             try {
-                if(null != httpResponse){
+                if (null != httpResponse) {
                     httpResponse.close();
                 }
-            }catch (Exception e){
+            } catch (Exception e) {
                 e.printStackTrace();
             }
         }

--
Gitblit v1.7.1