Httpclient使用opensslPkcs12證書的簡單說明-微信紅包接口

 1.下載微信pkcs12證書

/***
	 * @param filePath            文件的路徑
	 * @param password            商戶密碼
	 * @return
	 * @throws Exception
	 */
	private SSLContext getSSLContext(String filePath, char[] password) throws Exception {
		InputStream inputStream = this.getClass().getResourceAsStream(filePath);
		KeyStore keystore = KeyStore.getInstance("PKCS12");
		keystore.load(inputStream, password);
		SSLContext sslContext = SSLContexts.custom().loadKeyMaterial(keystore, password).build();
		return sslContext;
	}

2.將sslContext導入httpClient中

HttpClientBuilder builder = HttpClients.custom();

//如下方法已通過時,待深究
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
          getSSLContext(),
          new String[] { "TLSv1" },
          null,
          SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);

builder.setSSLSocketFactory(sslsf);
//完成導入
CloseableHttpClient httpClient = builder.build();

3.使用httpclient

參考java

相關文章
相關標籤/搜索