leutu
2024-05-08 543e4eb01ca210b20876e8139cb3d0403d7d065c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package com.skyline.electricity.utils;
 
import org.springframework.http.client.*;
 
import java.io.IOException;
import java.net.*;
import java.security.*;
import java.security.cert.X509Certificate;
import javax.net.ssl.*;
 
/*public class HttpsClientRequestFactory extends SimpleClientHttpRequestFactory
{
    @Override
    protected void prepareConnection(final HttpURLConnection connection, final String httpMethod) {
        try {
            if (!(connection instanceof HttpsURLConnection)) {
                throw new RuntimeException("An instance of HttpsURLConnection is expected");
            }
            final HttpsURLConnection httpsConnection = (HttpsURLConnection)connection;
            final TrustManager[] trustAllCerts = { (TrustManager)new HttpsClientRequestFactory.HttpsClientRequestFactory(this) };
            final SSLContext sslContext = SSLContext.getInstance("TLS");
            sslContext.init(null, trustAllCerts, new SecureRandom());
            httpsConnection.setSSLSocketFactory((SSLSocketFactory)new HttpsClientRequestFactory.MyCustomSSLSocketFactory(sslContext.getSocketFactory()));
            httpsConnection.setHostnameVerifier((HostnameVerifier)new HttpsClientRequestFactory.HttpsClientRequestFactory$2(this));
            super.prepareConnection((HttpURLConnection)httpsConnection, httpMethod);
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
}*/
/**
 * 兼容调Https接口
 */
public class HttpsClientRequestFactory extends SimpleClientHttpRequestFactory {
 
    @Override
    protected void prepareConnection(HttpURLConnection connection, String httpMethod)
            throws IOException {
        if (connection instanceof HttpsURLConnection) {
            prepareHttpsConnection((HttpsURLConnection) connection);
        }
        super.prepareConnection(connection, httpMethod);
    }
 
    private void prepareHttpsConnection(HttpsURLConnection connection) {
        connection.setHostnameVerifier(new SkipHostnameVerifier());
        try {
            connection.setSSLSocketFactory(createSslSocketFactory());
        }
        catch (Exception ex) {
            // Ignore
        }
    }
 
    private SSLSocketFactory createSslSocketFactory() throws Exception {
        SSLContext context = SSLContext.getInstance("TLS");
        context.init(null, new TrustManager[] { new SkipX509TrustManager() },
                new SecureRandom());
        return context.getSocketFactory();
    }
 
    private class SkipHostnameVerifier implements HostnameVerifier {
 
        @Override
        public boolean verify(String s, SSLSession sslSession) {
            return true;
        }
 
    }
 
    private static class SkipX509TrustManager implements X509TrustManager {
 
        @Override
        public X509Certificate[] getAcceptedIssuers() {
            return new X509Certificate[0];
        }
 
        @Override
        public void checkClientTrusted(X509Certificate[] chain, String authType) {
        }
 
        @Override
        public void checkServerTrusted(X509Certificate[] chain, String authType) {
        }
 
    }
}