Apache HTTPClient 忽略证书
程序员文章站
2022-06-21 21:06:46
...
/** * httpclient4.5.2版 * 忽略服务器证书,采用信任机制 * @return */ public static HttpClientConnectionManager init(){ try { SSLContext sslContext = SSLContexts.custom().loadTrustMaterial(null, new TrustStrategy() { @Override public boolean isTrusted(X509Certificate[] arg0, String arg1) throws CertificateException { return true; } }).build(); SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory( sslContext, new String[] { "TLSv1" }, null, SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); Registry registry = RegistryBuilder . create() .register("http", PlainConnectionSocketFactory.INSTANCE) .register("https", sslsf).build(); return new PoolingHttpClientConnectionManager(registry); } catch (Exception e) { e.printStackTrace(); } return null; }
/** * javax.net.ssl * 忽略服务器证书,采用信任机制 */ private static void disableSslVerification() { try { // Create a trust manager that does not validate certificate chains TrustManager[] trustAllCerts = new TrustManager[]{new X509TrustManager() { public java.security.cert.X509Certificate[] getAcceptedIssuers() { return null; } public void checkClientTrusted(X509Certificate[] certs, String authType) { } public void checkServerTrusted(X509Certificate[] certs, String authType) { } } }; // Install the all-trusting trust manager SSLContext sc = SSLContext.getInstance("SSL"); sc.init(null, trustAllCerts, new java.security.SecureRandom()); HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory()); // Create all-trusting host name verifier HostnameVerifier allHostsValid = new HostnameVerifier() { public boolean verify(String hostname, SSLSession session) { return true; } }; // Install the all-trusting host verifier HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (KeyManagementException e) { e.printStackTrace(); } }
下一篇: 水果如何减肥瘦身 你知道如何排毒吗
推荐阅读
-
PHPStudy下如何为Apache安装SSL证书的方法步骤
-
centos apache 腾讯云ssl证书配置
-
JSP开发中Apache-HTTPClient 用户验证的实例详解
-
PHPStudy下如何为Apache安装SSL证书的方法步骤
-
centos apache 腾讯云ssl证书配置
-
Apache环境下配置多个ssl证书搭建多个站点的方法
-
.net core signalR 忽略https 证书
-
httpclient org.apache.http.NoHttpResponseException: host:端口 failed to respond 错误原因和解决方法
-
详解在Ubuntu上的Apache配置SSL(https证书)的正确姿势
-
httpclient 忽略https,信任所有证书