碼迷,mamicode.com
首頁 > 微信 > 詳細

微信支付 帶apiclient_cert.p12證書的請求方法 JAVA版

時間:2018-05-12 14:24:18      閱讀:1925      評論:0      收藏:0      [點我收藏+]

標簽:instance   sea   默認   head   exception   tostring   ons   微信   warning   

以下是帶apiclient_cert.p12證書的請求方法

package utils.wechat;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.security.KeyStore;

import javax.net.ssl.SSLContext;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLContexts;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

/**
 * 獲取微信apiclient_cert.p12證書
 * 
 * @author zengwei
 * @email [email protected]
 * @date 2018年4月5日 下午4:49:36
 */
@SuppressWarnings("deprecation")
public class CertHttpUtil {

    private static int socketTimeout = 10000;// 連接超時時間,默認10秒
    private static int connectTimeout = 30000;// 傳輸超時時間,默認30秒
    private static RequestConfig requestConfig;// 請求器的配置
    private static CloseableHttpClient httpClient;// HTTP請求器

    /**
     * 通過Https往API post xml數據
     * 
     * @param url API地址
     * @param xmlObj 要提交的XML數據對象
    * @param mchId 商戶ID
    * @param certPath 證書位置
     * @return
     */
    public static String postData(String url, String xmlObj, String mchId, String certPath) {
        // 加載證書
        try {
            initCert(mchId, certPath);
        } catch (Exception e) {
            e.printStackTrace();
        }
        String result = null;
        HttpPost httpPost = new HttpPost(url);
        // 得指明使用UTF-8編碼,否則到API服務器XML的中文不能被成功識別
        StringEntity postEntity = new StringEntity(xmlObj, "UTF-8");
        httpPost.addHeader("Content-Type", "text/xml");
        httpPost.setEntity(postEntity);
        // 根據默認超時限制初始化requestConfig
        requestConfig = RequestConfig.custom().setSocketTimeout(socketTimeout).setConnectTimeout(connectTimeout).build();
        // 設置請求器的配置
        httpPost.setConfig(requestConfig);
        try {
            HttpResponse response = null;
            try {
                response = httpClient.execute(httpPost);
            } catch (IOException e) {
                e.printStackTrace();
            }
            HttpEntity entity = response.getEntity();
            try {
                result = EntityUtils.toString(entity, "UTF-8");
            } catch (IOException e) {
                e.printStackTrace();
            }
        } finally {
            httpPost.abort();
        }
        return result;
    }

    /**
     * 加載證書
     * 
     * @param mchId 商戶ID
     * @param certPath 證書位置
     * @throws Exception
     */
    private static void initCert(String mchId, String certPath) throws Exception {
        // 證書密碼,默認為商戶ID
        String key = mchId;
        // 證書的路徑
        String path = certPath;
        // 指定讀取證書格式為PKCS12
        KeyStore keyStore = KeyStore.getInstance("PKCS12");
        // 讀取本機存放的PKCS12證書文件
        FileInputStream instream = new FileInputStream(new File(path));
        try {
            // 指定PKCS12的密碼(商戶ID)
            keyStore.load(instream, key.toCharArray());
        } finally {
            instream.close();
        }
        SSLContext sslcontext = SSLContexts.custom().loadKeyMaterial(keyStore, key.toCharArray()).build();
        SSLConnectionSocketFactory sslsf =
                new SSLConnectionSocketFactory(sslcontext, new String[] {"TLSv1"}, null,
                        SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
        httpClient = HttpClients.custom().setSSLSocketFactory(sslsf).build();
    }
}

  

微信支付 帶apiclient_cert.p12證書的請求方法 JAVA版

標簽:instance   sea   默認   head   exception   tostring   ons   微信   warning   

原文地址:https://www.cnblogs.com/zengweiB208/p/9025545.html

(0)
(0)
   
舉報
評論 一句話評論(0
0條  
登錄后才能評論!
? 2014 mamicode.com 版權所有 京ICP備13008772號-2
迷上了代碼!
公式规律下期单双