微信支付 帶apiclient_cert.p12證書的請求方法 JAVA版
阿新 • • 發佈:2018-05-12
sls std return key AR import init p12證書 下午
以下是帶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版