http 4.3以前的
HttpClient httpClient = httpClient1;
X509TrustManager xtm = new X509TrustManager() {
public void checkClientTrusted(X509Certificate[] chain,
String authType) throws CertificateException {
}
public void checkServerTrusted(X509Certificate[] chain,
String authType) throws CertificateException {
}
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[] {};
}
};
try {
SSLContext ctx = SSLContext.getInstance("SSL");
ctx.init(null, new TrustManager[] { xtm }, null);
SSLSocketFactory sf = new SSLSocketFactory(
ctx,
SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
Scheme sch = new Scheme("https", 443, sf);
httpClient.getConnectionManager().getSchemeRegistry().register(sch);
HttpGet httpPost = new HttpGet(url);
httpPost.setHeader("content-type", contextType);
HttpResponse response = httpClient.execute(httpPost);
HttpEntity entity = response.getEntity();
bs = IOUtils.toByteArray(entity.getContent());
if (null != entity) {
EntityUtils.consume(entity);
}
return bs;
} catch (Exception e) {
e.printStackTrace();
} finally {
}
return bs;
}
==========================================================================================================
4.3以后
SSLContext sslContext;
try {
sslContext = new SSLContextBuilder().loadTrustMaterial(null,
new TrustStrategy() {
// 信任所有
public boolean isTrusted(X509Certificate[] chain,
String authType) throws CertificateException {
return true;
}
}).build();
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
sslContext,
SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
httpclient = HttpClients.custom()
.setSSLSocketFactory(sslsf).build();
HttpPost httpPost = new HttpPost(httpAddr2);
//httpclient构建完成 构造请求参数
List<NameValuePair> formparams = new ArrayList<NameValuePair>();
formparams.add(new BasicNameValuePair("SENDER_CODE", spid));
formparams.add(new BasicNameValuePair("TRX_CONTENT", base64Str));
formparams.add(new BasicNameValuePair("SIGNATURE", md5Str));
UrlEncodedFormEntity uefEntity;
uefEntity = new UrlEncodedFormEntity(formparams);
httpPost.setEntity(uefEntity);
CloseableHttpResponse response = httpclient.execute(httpPost);
HttpEntity entity = response.getEntity();