0
点赞
收藏
分享

微信扫一扫

SSL httpclient

登高且赋 2022-08-04 阅读 79


public static HttpClient getHttpClientWithSSL(String SSLPath) 
throws KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException, KeyManagementException, UnrecoverableKeyException{

HttpClient httpclient = new DefaultHttpClient();
// SSLContext ctx = SSLContext.getInstance("SSL");

// Implementation of a trust manager for X509 certificates
/*X509TrustManager tm = new X509TrustManager() {

public void checkClientTrusted(X509Certificate[] xcs,
String string) throws CertificateException {

}

public void checkServerTrusted(X509Certificate[] xcs,
String string) throws CertificateException {
}

public X509Certificate[] getAcceptedIssuers() {
return null;
}
};*/

/*X509HostnameVerifier hostnameVerifier = new X509HostnameVerifier() {

public boolean verify(String hostname, SSLSession session) {
// TODO Auto-generated method stub
return true;
}

public void verify(String arg0, SSLSocket arg1)
throws IOException {
// TODO Auto-generated method stub

}

public void verify(String arg0, X509Certificate arg1)
throws SSLException {
// TODO Auto-generated method stub

}

public void verify(String arg0, String[] arg1, String[] arg2)
throws SSLException {
// TODO Auto-generated method stub

}
};*/
// ctx.init(null, new TrustManager[] { tm }, null);

KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
FileInputStream instream = new FileInputStream(new File(SSLPath));
try {
trustStore.load(instream, "123456".toCharArray());
} finally {
instream.close();
}

// SSLSocketFactory ssf = new SSLSocketFactory(ctx, hostnameVerifier);

SSLSocketFactory socketFactory = new SSLSocketFactory(trustStore,"123456",trustStore);
// Scheme sch = new Scheme("https", socketFactory, 443);
Scheme sch = new Scheme("https", 8443, socketFactory);
httpclient.getConnectionManager().getSchemeRegistry().register(sch);


// httpclient.getConnectionManager().shutdown();

return httpclient;
}

 

举报

相关推荐

0 条评论