一、定义类:
public class DefaultTrustManager implements X509TrustManager {
@Override
public X509Certificate[] getAcceptedIssuers() {
return null;
}
@Override
public void checkClientTrusted(X509Certificate[] chain, String authType)
throws CertificateException {
}
@Override
public void checkServerTrusted(X509Certificate[] chain, String authType)
throws CertificateException {
}
}
二、调用代码
HttpClient client = new DefaultHttpClient();
SSLContext ctx = SSLContext.getInstance("TLS");
ctx.init(new KeyManager[0],
new TrustManager[] {new DefaultTrustManager()},
new SecureRandom());
SSLSocketFactory sf = new SSLSocketFactory(
ctx,
SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
Scheme https = new Scheme("https", 8443, sf);
client.getConnectionManager().getSchemeRegistry().register(https);
// 往下即可使用https通信,调用方法和http没有区别。
public class DefaultTrustManager implements X509TrustManager {
@Override
public X509Certificate[] getAcceptedIssuers() {
return null;
}
@Override
public void checkClientTrusted(X509Certificate[] chain, String authType)
throws CertificateException {
}
@Override
public void checkServerTrusted(X509Certificate[] chain, String authType)
throws CertificateException {
}
}
二、调用代码
HttpClient client = new DefaultHttpClient();
SSLContext ctx = SSLContext.getInstance("TLS");
ctx.init(new KeyManager[0],
new TrustManager[] {new DefaultTrustManager()},
new SecureRandom());
SSLSocketFactory sf = new SSLSocketFactory(
ctx,
SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
Scheme https = new Scheme("https", 8443, sf);
client.getConnectionManager().getSchemeRegistry().register(https);
// 往下即可使用https通信,调用方法和http没有区别。
本文介绍如何在HTTP客户端中通过自定义信任管理器实现HTTPS通信,包括SSL上下文初始化、SSLSocketFactory配置及注册自定义HTTPS方案。
1978

被折叠的 条评论
为什么被折叠?



