如何忽略Apache HttpClient 4.0中的SSL证书错误

如何绕过Apache HttpClient 4.0的无效SSL证书错误?

回答:

你需要使用自己的TrustManager创建SSLContext并使用此上下文创建HTTPS方案。这是代码,

SSLContext sslContext = SSLContext.getInstance("SSL");

// set up a TrustManager that trusts everything

sslContext.init(null, new TrustManager[] { new X509TrustManager() {

public X509Certificate[] getAcceptedIssuers() {

System.out.println("getAcceptedIssuers =============");

return null;

}

public void checkClientTrusted(X509Certificate[] certs,

String authType) {

System.out.println("checkClientTrusted =============");

}

public void checkServerTrusted(X509Certificate[] certs,

String authType) {

System.out.println("checkServerTrusted =============");

}

} }, new SecureRandom());

SSLSocketFactory sf = new SSLSocketFactory(sslContext);

Scheme httpsScheme = new Scheme("https", 443, sf);

SchemeRegistry schemeRegistry = new SchemeRegistry();

schemeRegistry.register(httpsScheme);

// apache HttpClient version >4.2 should use BasicClientConnectionManager

ClientConnectionManager cm = new SingleClientConnManager(schemeRegistry);

HttpClient httpClient = new DefaultHttpClient(cm);

以上是 如何忽略Apache HttpClient 4.0中的SSL证书错误 的全部内容, 来源链接: utcz.com/qa/428194.html

回到顶部