代码如下:修改证书路径跟密码即可
KeyStore keyStore = KeyStore.getInstance("PKCS12");
FileInputStream instream = new FileInputStream(new File("证书路径如(D://aa.p12)"));
keyStore.load(instream, "证书密码(初始是商户 ID)".toCharArray());//杩欓噷鍐欏
instream.close();
SSLContext sslcontext = SSLContexts.custom().loadKeyMaterial(keyStore, "证书密码(初始是商户 ID)".toCharArray()).build();
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext,new String[] { "TLSv1" },null,
SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
CloseableHttpClient httpclient = HttpClients.custom() .setSSLSocketFactory(sslsf) .build();
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.");
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);
HttpEntity entity = response.getEntity();
String jsonStr = EntityUtils .toString(response.getEntity(), "UTF-8");
EntityUtils.consume(entity);