先上代码
public class HttpsUtil {
private Logger logger = LoggerFactory.getLogger(HttpsUtil.class);
private static final ClassPathResource KEY_STORE_CLIENT_PATH = new ClassPathResource("ca/client.p12");
private static final String KEY_STORE_TYPE_JKS = "JKS";
private static final String KEYSTORE_PASSWORD = "123456";
private CloseableHttpClient httpClient;
public HttpsUtil() throws Exception {
KeyStore keyStore = KeyStore.getInstance(KEY_STORE_TYPE_JKS);
KeyStore trustKeyStore = KeyStore.getInstance(KeyStore.getDefaultType());
InputStream instream = KEY_STORE_CLIENT_PATH.getInputStream();
try {
keyStore.load(instream, KEYSTORE_PASSWORD.toCharArray());
} catch (CertificateException e) {
logger.error("加载客户端端可信任证书出错了", e);
} finally {
try {
instream.close();
} catch (Exception ignore) {
}
}
SSLContext sslcontext = SSLContexts.custom()
.loadTrustMaterial(new TrustStrategy() {
@Override
public boolean isTrusted(java.security.cert.X509Certificate[] chain, String authType) throws CertificateException {
return true;
}
})
.loadKeyMaterial(keyStore, KEYSTORE_PASSWORD.toCharArray())
.build();
SSLConnectionSocketFactory sslConnectionSocketFactory = new SSLConnectionSocketFactory(
sslcontext,
new String[]{"TLSv1.1"},
null,
SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
this.httpClient = HttpClients.custom()
.setSSLSocketFactory(sslConnectionSocketFactory)
.build();
}
public String post(String url, Map<String, Object> map) throws Exception {
HttpPost httpPost = new HttpPost(url);
if (null != map) {
List<NameValuePair> params = new ArrayList<NameValuePair>();
for (Map.Entry<String, Object> entry : map.entrySet()) {
params.add(new BasicNameValuePair(entry.getKey(), entry.getValue().toString()));
}
UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(params, "utf-8");
formEntity.setContentType("Content-Type:application/json");
httpPost.setEntity(formEntity);
}
CloseableHttpResponse response = this.httpClient.execute(httpPost);
HttpEntity entity = response.getEntity();
String res = EntityUtils.toString(entity, Charset.defaultCharset());
EntityUtils.consume(entity);
return res;
}
public String get(String url, Map<String, Object> map) throws Exception {
String params = null;
if (null != map) {
List<NameValuePair> list = new ArrayList<NameValuePair>();
for (Map.Entry<String, Object> entry : map.entrySet()) {
list.add(new BasicNameValuePair(entry.getKey(), entry.getValue().toString()));
}
params = EntityUtils.toString(new UrlEncodedFormEntity(list, "utf-8"));
}
url += StringUtils.isNotBlank(params) ? ("?" + params) : "";
HttpGet httpGet = new HttpGet(url);
CloseableHttpResponse response = this.httpClient.execute(httpGet);
HttpEntity entity = response.getEntity();
String res = EntityUtils.toString(entity, Charset.defaultCharset());
EntityUtils.consume(entity);
response.close();
return res;
}
public String post(String url, String json) throws IOException {
HttpPost httpPost = new HttpPost(url);
httpPost.setHeader("Content-Type", "application/json;charset=UTF-8");
httpPost.setHeader("Accept", "application/json");
StringEntity param = new StringEntity(json, ContentType.APPLICATION_JSON);
httpPost.setEntity(param);
CloseableHttpResponse resp = this.httpClient.execute(httpPost);
HttpEntity entity = resp.getEntity();
String res = EntityUtils.toString(entity, Charset.defaultCharset());
EntityUtils.consume(entity);
resp.close();
return res;
}
}