你可以编写一个 TrustManager 类来处理它。例:
ClientConnectionManager cm = new BasicClientConnectionManager();
cm.getSchemeRegistry().register(createHttpsScheme());
DefaultHttpClient client = new DefaultHttpClient(cm);
String url = "https://your domain/your url";
HttpGet get = new HttpGet(url);
HttpResponse resp = client.execute(get);
etc..
public static Scheme createHttpsScheme() {
SSLContext context = SSLContext.getInstance("TLS");
context.init(null, new TrustManager[] {
new TestTrustManager()
}, new SecureRandom());
SSLSocketFactory sf = new SSLSocketFactory(context);
return new Scheme("https", 443, sf);
}
int TestTrustManager.java你可以像这样打印链:
public class TestTrustManager implements X509TrustManager {
@Override
public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
for (int i = 0; i < chain.length; ++i) {
System.out.println(chain[i]);
}
decorated.checkServerTrusted(chain, authType);
}
}