1
+ package com .loopj .android .http ;
2
+
3
+ import org .apache .http .conn .ssl .SSLSocketFactory ;
4
+
5
+ import java .io .IOException ;
6
+ import java .net .Socket ;
7
+ import java .net .UnknownHostException ;
8
+ import java .security .KeyManagementException ;
9
+ import java .security .KeyStore ;
10
+ import java .security .KeyStoreException ;
11
+ import java .security .NoSuchAlgorithmException ;
12
+ import java .security .UnrecoverableKeyException ;
13
+
14
+ import javax .net .ssl .SSLContext ;
15
+ import javax .net .ssl .TrustManager ;
16
+ import javax .net .ssl .X509TrustManager ;
17
+
18
+ /**
19
+ * This file is introduced to fix HTTPS Post bug on API < ICS
20
+ * see http://code.google.com/p/android/issues/detail?id=13117#c14
21
+ */
22
+ public class MySSLSocketFactory extends SSLSocketFactory {
23
+ SSLContext sslContext = SSLContext .getInstance ("TLS" );
24
+
25
+ public MySSLSocketFactory (KeyStore truststore )
26
+ throws NoSuchAlgorithmException , KeyManagementException ,
27
+ KeyStoreException , UnrecoverableKeyException {
28
+ super (truststore );
29
+
30
+ TrustManager tm = new X509TrustManager () {
31
+ public java .security .cert .X509Certificate [] getAcceptedIssuers () {
32
+ return null ;
33
+ }
34
+
35
+ @ Override
36
+ public void checkClientTrusted (
37
+ java .security .cert .X509Certificate [] chain , String authType )
38
+ throws java .security .cert .CertificateException {
39
+ }
40
+
41
+ @ Override
42
+ public void checkServerTrusted (
43
+ java .security .cert .X509Certificate [] chain , String authType )
44
+ throws java .security .cert .CertificateException {
45
+ }
46
+ };
47
+ sslContext .init (null , new TrustManager []{tm }, null );
48
+ }
49
+
50
+ @ Override
51
+ public Socket createSocket (Socket socket , String host , int port , boolean autoClose ) throws IOException , UnknownHostException {
52
+ return sslContext .getSocketFactory ().createSocket (socket , host , port , autoClose );
53
+ }
54
+
55
+ @ Override
56
+ public Socket createSocket () throws IOException {
57
+ return sslContext .getSocketFactory ().createSocket ();
58
+ }
59
+
60
+ public static KeyStore getKeystore () {
61
+ KeyStore trustStore = null ;
62
+ try {
63
+ trustStore = KeyStore .getInstance (KeyStore .getDefaultType ());
64
+ trustStore .load (null , null );
65
+ } catch (Throwable t ) {
66
+ t .printStackTrace ();
67
+ }
68
+ return trustStore ;
69
+ }
70
+
71
+ public static SSLSocketFactory getFixedSocketFactory () {
72
+ SSLSocketFactory socketFactory ;
73
+ try {
74
+ socketFactory = new MySSLSocketFactory (getKeystore ());
75
+ socketFactory .setHostnameVerifier (SSLSocketFactory .ALLOW_ALL_HOSTNAME_VERIFIER );
76
+ } catch (Throwable t ) {
77
+ t .printStackTrace ();
78
+ socketFactory = SSLSocketFactory .getSocketFactory ();
79
+ }
80
+ return socketFactory ;
81
+ }
82
+
83
+ }
0 commit comments