|
27 | 27 | import org.apache.http.client.methods.HttpRequestBase;
|
28 | 28 | import org.apache.http.protocol.HTTP;
|
29 | 29 |
|
30 |
| -import javax.net.ssl.HttpsURLConnection; |
31 |
| -import javax.net.ssl.SSLContext; |
32 |
| -import javax.net.ssl.TrustManager; |
33 |
| -import javax.net.ssl.X509TrustManager; |
| 30 | +import javax.net.ssl.*; |
34 | 31 | import java.io.File;
|
35 | 32 | import java.io.UnsupportedEncodingException;
|
36 | 33 | import java.lang.reflect.Field;
|
@@ -228,32 +225,39 @@ public static StackTraceElement getCallerStackTraceElement() {
|
228 | 225 | return Thread.currentThread().getStackTrace()[4];
|
229 | 226 | }
|
230 | 227 |
|
231 |
| - private static TrustManager[] trustAllCerts; |
| 228 | + private static SSLSocketFactory sslSocketFactory; |
232 | 229 |
|
233 |
| - public static void trustAllSSLForHttpsURLConnection() { |
| 230 | + public static void trustAllHttpsURLConnection() { |
234 | 231 | // Create a trust manager that does not validate certificate chains
|
235 |
| - if (trustAllCerts == null) { |
236 |
| - trustAllCerts = new TrustManager[]{new X509TrustManager() { |
| 232 | + if (sslSocketFactory == null) { |
| 233 | + TrustManager[] trustAllCerts = new TrustManager[]{new X509TrustManager() { |
| 234 | + @Override |
237 | 235 | public java.security.cert.X509Certificate[] getAcceptedIssuers() {
|
238 | 236 | return null;
|
239 | 237 | }
|
240 | 238 |
|
241 |
| - public void checkClientTrusted(X509Certificate[] certs, String authType) { |
| 239 | + @Override |
| 240 | + public void checkClientTrusted(X509Certificate[] certs, |
| 241 | + String authType) { |
242 | 242 | }
|
243 | 243 |
|
244 |
| - public void checkServerTrusted(X509Certificate[] certs, String authType) { |
| 244 | + @Override |
| 245 | + public void checkServerTrusted(X509Certificate[] certs, |
| 246 | + String authType) { |
245 | 247 | }
|
246 | 248 | }};
|
| 249 | + try { |
| 250 | + SSLContext sslContext = SSLContext.getInstance("TLS"); |
| 251 | + sslContext.init(null, trustAllCerts, null); |
| 252 | + sslSocketFactory = sslContext.getSocketFactory(); |
| 253 | + } catch (Throwable e) { |
| 254 | + LogUtils.e(e.getMessage(), e); |
| 255 | + } |
247 | 256 | }
|
248 |
| - // Install the all-trusting trust manager |
249 |
| - final SSLContext sslContext; |
250 |
| - try { |
251 |
| - sslContext = SSLContext.getInstance("TLS"); |
252 |
| - sslContext.init(null, trustAllCerts, null); |
253 |
| - HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory()); |
254 |
| - } catch (Throwable e) { |
255 |
| - LogUtils.e(e.getMessage(), e); |
| 257 | + |
| 258 | + if (sslSocketFactory != null) { |
| 259 | + HttpsURLConnection.setDefaultSSLSocketFactory(sslSocketFactory); |
| 260 | + HttpsURLConnection.setDefaultHostnameVerifier(org.apache.http.conn.ssl.SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); |
256 | 261 | }
|
257 |
| - HttpsURLConnection.setDefaultHostnameVerifier(org.apache.http.conn.ssl.SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); |
258 | 262 | }
|
259 | 263 | }
|
0 commit comments