微信支付请求(带证书)

import cn.hutool.core.lang.Pair;
import cn.hutool.crypto.SecureUtil;
import com.dtyunxi.exchange.biz.model.ApiChannelType;
import com.dtyunxi.exchange.biz.utils.OkHttpUtil;
import okhttp3.OkHttpClient;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
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.HttpClients;
import org.apache.http.ssl.SSLContexts;
import org.apache.http.util.EntityUtils;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Component;
import org.springframework.util.Base64Utils;
import javax.net.ssl.SSLContext;
import java.io.*;
import java.security.KeyStore;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

    private static final String SslContentKey = "certBase64Content";
    private Map<String, CloseableHttpClient> OkHttpClientMap = new HashMap<>();

    
    public Pair<Integer, String> execute(Map<String, Object> requestContext, HttpMethod httpMethod, String url, Map<String, String> headers, Object postData) throws IOException {
        String base64Content = requestContext.get(SslContentKey).toString();
        String mchId = requestContext.get("mchId").toString();
        String key = mchId;
        CloseableHttpClient httpClient = OkHttpClientMap.get(key);
        if (httpClient == null) {
            SSLContext sslContext = initSSLContext(mchId, base64Content);
            SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext,
                    null,//new String[]{"TLSv1"},
                    (String[]) null,
                    SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
            httpClient = HttpClients.custom().setSSLSocketFactory(sslsf).build();
            ;
            OkHttpClientMap.put(key, httpClient);
        }

        String result = null;
        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(postData.toString(), "UTF-8"));
            CloseableHttpResponse response = null;
            try {
                response = httpClient.execute(httpost);
                HttpEntity entity = response.getEntity();
                String jsonStr = EntityUtils.toString(response.getEntity(), "UTF-8");
                EntityUtils.consume(entity);
                result = jsonStr;
            } catch (Exception ex) {
                ex.printStackTrace();
                return Pair.of(500, ex.getMessage());
            } finally {
                if (response != null) {
                    response.close();
                }
            }
        } finally {
            //httpClient.close();
        }

        return Pair.of(200, result);
        //return OkHttpUtil.execute(httpMethod, url, headers, postData);
    }

 

posted @ 2023-08-07 19:17  zslm___  阅读(97)  评论(0编辑  收藏  举报