潘志宝
2024-12-15 c50decb8e57c032f7bb8c52565ce8b8dece27441
提交 | 用户 | 时间
a6de49 1 package com.iailab.module.data.common.utils;
H 2
3
4 import org.apache.http.conn.ClientConnectionManager;
5 import org.apache.http.conn.scheme.Scheme;
6 import org.apache.http.conn.scheme.SchemeRegistry;
7 import org.apache.http.conn.ssl.SSLSocketFactory;
8 import org.apache.http.impl.client.DefaultHttpClient;
9
10 import javax.net.ssl.SSLContext;
11 import javax.net.ssl.TrustManager;
12 import javax.net.ssl.X509TrustManager;
13 import java.security.cert.CertificateException;
14 import java.security.cert.X509Certificate;
15
16 /**
17  * @author PanZhibao
18  * @date 2021年06月03日 15:35
19  */
20 public class SSLClient extends DefaultHttpClient {
21     public SSLClient() throws Exception {
22         super();
23         //传输协议需要根据自己的判断
24         SSLContext ctx = SSLContext.getInstance("TLS");
25         X509TrustManager tm = new X509TrustManager() {
26             @Override
27             public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
28             }
29
30             @Override
31             public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
32             }
33
34             @Override
35             public X509Certificate[] getAcceptedIssuers() {
36                 return null;
37             }
38         };
39         ctx.init(null, new TrustManager[]{tm}, null);
40         SSLSocketFactory ssf = new SSLSocketFactory(ctx, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
41         ClientConnectionManager ccm = this.getConnectionManager();
42         SchemeRegistry sr = ccm.getSchemeRegistry();
43         sr.register(new Scheme("https", 443, ssf));
44     }
45 }