From 19e7bbc520b9227b53d86b9bf947f6de7661f27f Mon Sep 17 00:00:00 2001
From: lidongdong <1459917685@qq.com>
Date: 星期五, 20 十月 2023 09:47:56 +0800
Subject: [PATCH] 修改商家服务

---
 springcloud_k8s_panzhihuazhihuishequ/common/src/main/java/com/panzhihua/common/utlis/HttpClientUtil.java |  145 +++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 142 insertions(+), 3 deletions(-)

diff --git a/springcloud_k8s_panzhihuazhihuishequ/common/src/main/java/com/panzhihua/common/utlis/HttpClientUtil.java b/springcloud_k8s_panzhihuazhihuishequ/common/src/main/java/com/panzhihua/common/utlis/HttpClientUtil.java
index 3830987..de79f95 100644
--- a/springcloud_k8s_panzhihuazhihuishequ/common/src/main/java/com/panzhihua/common/utlis/HttpClientUtil.java
+++ b/springcloud_k8s_panzhihuazhihuishequ/common/src/main/java/com/panzhihua/common/utlis/HttpClientUtil.java
@@ -4,27 +4,35 @@
 import java.net.MalformedURLException;
 import java.net.URL;
 import java.net.URLConnection;
+import java.security.KeyStore;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 
+import com.alibaba.fastjson.JSONObject;
 import org.apache.http.Header;
 import org.apache.http.HttpEntity;
 import org.apache.http.HttpResponse;
 import org.apache.http.client.HttpClient;
+import org.apache.http.client.methods.CloseableHttpResponse;
 import org.apache.http.client.methods.HttpGet;
 import org.apache.http.client.methods.HttpPost;
 import org.apache.http.client.methods.HttpUriRequest;
+import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
 import org.apache.http.entity.StringEntity;
+import org.apache.http.impl.client.CloseableHttpClient;
 import org.apache.http.impl.client.DefaultHttpClient;
 import org.apache.http.impl.client.HttpClients;
 import org.apache.http.message.BasicHeader;
+import org.apache.http.ssl.SSLContexts;
 import org.apache.http.util.EntityUtils;
 import org.springframework.util.ObjectUtils;
 
 import com.panzhihua.common.constants.HttpConstant;
 
 import lombok.extern.slf4j.Slf4j;
+
+import javax.net.ssl.SSLContext;
 
 /**
  * @program: springcloud_k8s_panzhihuazhihuishequ
@@ -76,7 +84,7 @@
             stringEntity.setContentEncoding(HttpConstant.UTF8_ENCODE);
             stringEntity.setContentType(HttpConstant.APPLICATION_JSON);
             httpPost.setEntity(stringEntity);
-            log.info("请求{}接口的参数为{}", url, jsonParam);
+//            log.info("请求{}接口的参数为{}", url, jsonParam);
             // 执行发送,获取相应结果
             httpResponse = httpClient.execute(httpPost);
             httpEntity = httpResponse.getEntity();
@@ -99,7 +107,7 @@
         }
         // 请求接口的响应时间
         endTime = System.currentTimeMillis();
-        log.info("请求{}接口的响应报文内容为{},本次请求API接口的响应时间为:{}毫秒", url, result, (endTime - startTime));
+//        log.info("请求{}接口的响应报文内容为{},本次请求API接口的响应时间为:{}毫秒", url, result, (endTime - startTime));
         return result;
 
     }
@@ -224,7 +232,7 @@
         try {
             defaultHttpClient = new DefaultHttpClient();
             HttpPost httpPost = new HttpPost(url);
-            httpPost.setHeader("Content-Type", "application/json;charset=ut-8");
+            httpPost.setHeader("Content-Type", "application/json;charset=utf-8");
             if (StringUtils.isNotBlank(param)) {
                 log.info("参数值:{}", param);
                 HttpEntity httpEntity = new StringEntity(param, "utf-8");
@@ -254,6 +262,78 @@
             if (bufferedReader != null)
                 bufferedReader.close();
         }
+    }
+
+
+    /**
+     * http请求工具类,post请求
+     *
+     * @param url
+     *            url
+     * @param param
+     *            参数值 仅支持String
+     * @return
+     * @throws Exception
+     */
+    public static String httpPostAndToken(String url, String param,Map headerMap) throws Exception {
+        DefaultHttpClient defaultHttpClient = null;
+        BufferedReader bufferedReader = null;
+        try {
+            defaultHttpClient = new DefaultHttpClient();
+            HttpPost httpPost = new HttpPost(url);
+            httpPost.setHeader("Content-Type", "application/json;charset=utf-8");
+//            httpPost.setHeader("Authorization", headerMap.get("Authorization").toString());
+//            httpPost.setHeader("Host", headerMap.get("Host").toString());
+
+            for (Object key : headerMap.keySet()) {
+                httpPost.setHeader(key.toString(), headerMap.get(key).toString());
+            }
+            if (StringUtils.isNotBlank(param)) {
+                log.info("参数值:{}", param);
+                HttpEntity httpEntity = new StringEntity(param, "utf-8");
+                httpPost.setEntity(httpEntity);
+//                httpPost.setHeader("Content-Length", String.valueOf(httpEntity.getContentLength()));
+            }
+            HttpResponse httpResponse = defaultHttpClient.execute(httpPost);
+            if (httpResponse.getStatusLine().getStatusCode() != 200) {
+                int statusCode = httpResponse.getStatusLine().getStatusCode();
+                String errorLog = "请求失败,errorCode:" + httpResponse.getStatusLine().getStatusCode();
+                log.info(errorLog);
+                throw new Exception(url + errorLog);
+            }
+            // 读取返回信息
+            String output;
+            bufferedReader = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent(), "utf-8"));
+            StringBuilder stringBuilder = new StringBuilder();
+            while ((output = bufferedReader.readLine()) != null) {
+                stringBuilder.append(output);
+            }
+            log.info("调用接口返回的参数:" + stringBuilder.toString());
+            return stringBuilder.toString();
+        } catch (IOException e) {
+            e.printStackTrace();
+            throw e;
+        } finally {
+            if (defaultHttpClient != null)
+                defaultHttpClient.getConnectionManager().shutdown();
+            if (bufferedReader != null)
+                bufferedReader.close();
+        }
+    }
+
+    public static void main(String[] args) throws IOException {
+        DefaultHttpClient  defaultHttpClient = new DefaultHttpClient();
+        HttpPost httpPost = new HttpPost("https://dptest.d-power.com.cn:14404/v1/face");
+//        httpPost.setHeader("Content-Type", "application/json;charset=utf-8");
+        httpPost.setHeader("Authorization", "DpToken P3JHgjLbyljfLrFnS9OZbATRJmacdt4b");
+        httpPost.setHeader("Host", "123.60.2.66");
+//        httpPost.setHeader("Content-Length", String.valueOf(entity.getContentLength()));
+
+//        httpPost.setHeader("User-Agent", "Apache-HttpClient/4.5.12 (Java/1.8.0_162)[\\r][\\n]");
+        httpPost.setEntity(new StringEntity("{\"positions\":[{\"role\":\"occupant\",\"communityId\":\"64f99b2ed26106d4f0fe93f4\",\"unitId\":\"64f99b2ed26106d4f0fe93f7\"}],\"tel\":\"15696695118\",\"name\":\"四月里3\",\"timeout\":10,\"image\":\"fsdfsdfsdf\"}", "utf-8"));
+//        httpPost.setHeader("Content-Length", String.valueOf(entity.getContentLength()));
+        HttpResponse httpResponse = defaultHttpClient.execute(httpPost);
+        System.out.println(httpResponse.getEntity());
     }
 
     /**
@@ -332,4 +412,63 @@
         return in;
     }
 
+    /**
+     * 退款请求微信
+     * @param url   请求地址
+     * @param data  请求数据
+     * @param mchId 商户id
+     * @param isTest    是否是测试
+     * @return  退款结果
+     * @throws Exception    抛出异常
+     */
+    public static String doRefund(String url, String data,String mchId,Boolean isTest) throws Exception{
+        KeyStore keyStore  = KeyStore.getInstance("PKCS12");
+        //P12文件目录 证书路径,这里需要你自己修改,linux下还是windows下的根路径
+        String filepath = "/mnt/data/refund/huacheng/";
+
+        System.out.println("filepath->"+filepath);
+        FileInputStream instream = new FileInputStream(filepath+"apiclient_cert.p12");
+        try {
+            keyStore.load(instream, mchId.toCharArray());//这里写密码..默认是你的MCHID
+        } finally {
+            instream.close();
+        }
+
+        // Trust own CA and all self-signed certs
+        SSLContext sslcontext = SSLContexts.custom()
+                .loadKeyMaterial(keyStore, mchId.toCharArray())//这里也是写密码的
+                .build();
+        // Allow TLSv1 protocol only
+        SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
+                sslcontext,
+                SSLConnectionSocketFactory.getDefaultHostnameVerifier());
+        CloseableHttpClient httpclient = HttpClients.custom()
+                .setSSLSocketFactory(sslsf)
+                .build();
+        try {
+            HttpPost httpost = new HttpPost(url); // 设置响应头信息
+            httpost.addHeader("Connection", "keep-alive");
+            httpost.addHeader("Accept", "*/*");
+            httpost.addHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
+            httpost.addHeader("Host", "api.mch.weixin.qq.com");
+            httpost.addHeader("X-Requested-With", "XMLHttpRequest");
+            httpost.addHeader("Cache-Control", "max-age=0");
+            httpost.addHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0) ");
+            httpost.setEntity(new StringEntity(data, "UTF-8"));
+            CloseableHttpResponse response = httpclient.execute(httpost);
+            try {
+                HttpEntity entity = response.getEntity();
+
+                String jsonStr = EntityUtils.toString(response.getEntity(), "UTF-8");
+                log.info("请求微信退款接口返回结果:" + JSONObject.toJSONString(jsonStr));
+                EntityUtils.consume(entity);
+                return jsonStr;
+            } finally {
+                response.close();
+            }
+        } finally {
+            httpclient.close();
+        }
+
+    }
 }

--
Gitblit v1.7.1