微信企业付款获取RSA

package com.hentica.app.test.wx;

import com.plant.app.modules.pay.wxpay.config.WxpayConfig;
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 javax.net.ssl.SSLContext;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.security.KeyStore;

/**
 * 获取RSA
 */
public class TestT {

   
    public static void main(String[] args) throws Exception {
        String mchId = WxpayConfig.APP_ID;
        String key = WxpayConfig.APP_KEY;
        String certpassword = WxpayConfig.APP_CERT_PWD;
        PublicKeyData data = new PublicKeyData(mchId, "MD5", key);
        StringBuffer reqXml = new StringBuffer();
        reqXml.append("<xml>");
        reqXml.append("<mch_id>");
        reqXml.append(mchId);
        reqXml.append("</mch_id>");
        reqXml.append("<nonce_str>");
        reqXml.append(data.getNonce_str());
        reqXml.append("</nonce_str>");
        reqXml.append("<sign_type>");
        reqXml.append(data.getSign_type());
        reqXml.append("</sign_type>");
        reqXml.append("<sign>");
        reqXml.append(data.getSign());
        reqXml.append("</sign>");
        reqXml.append("</xml>");

        KeyStore keyStore = KeyStore.getInstance("PKCS12");
        FileInputStream instream = new FileInputStream(new File("D:/cert/apiclient_cert.p12"));//证书的路径
        try {
            keyStore.load(instream, mchId.toCharArray());
        } finally {
            instream.close();
        }

        SSLContext sslcontext = SSLContexts.custom().loadKeyMaterial(keyStore, mchId.toCharArray()).build();
        SSLConnectionSocketFactory sslf = new SSLConnectionSocketFactory(sslcontext);
        CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(sslf).build();

        try {

            HttpPost httpPost = new HttpPost("https://fraud.mch.weixin.qq.com/risk/getpublickey");//接口

            System.out.println("executing request" + httpPost.getRequestLine());
            StringEntity reqEntity = new StringEntity(reqXml.toString());
            // 设置类型
            reqEntity.setContentType("application/x-www-form-urlencoded");
            httpPost.setEntity(reqEntity);
            CloseableHttpResponse response = httpclient.execute(httpPost);
            try {
                HttpEntity entity = response.getEntity();
                if (entity != null) {
                    System.out.println("Response content length: " + entity.getContentLength());
                    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(entity.getContent(), "UTF-8"));
                    String line;
                    String xmlParam = "";
                    while ((line = bufferedReader.readLine()) != null) {
                        xmlParam += line;
                    }
                    System.out.println(xmlParam);
                }
                EntityUtils.consume(entity);
            } finally {
                response.close();
            }
        } finally {
            httpclient.close();
        }

    }
}

  参考地址:https://blog.csdn.net/qq_19167629/article/details/80668801

原文地址:https://www.cnblogs.com/james-roger/p/9985270.html