55import java .io .InputStreamReader ;
66import java .io .OutputStream ;
77import java .net .HttpURLConnection ;
8+ import java .net .SocketTimeoutException ;
89import java .net .URL ;
910import java .security .cert .CertificateException ;
1011import java .security .cert .X509Certificate ;
1112
1213import javax .net .ssl .HostnameVerifier ;
1314import javax .net .ssl .HttpsURLConnection ;
15+ import javax .net .ssl .SSLContext ;
1416import javax .net .ssl .SSLSession ;
1517import javax .net .ssl .TrustManager ;
1618import javax .net .ssl .X509TrustManager ;
@@ -30,6 +32,8 @@ public NativeHttpClient() {
3032 public NativeHttpClient (int maxRetryTimes ) {
3133 this ._maxRetryTimes = maxRetryTimes ;
3234 LOG .info ("Created instance with _maxRetryTimes = " + _maxRetryTimes );
35+
36+ initSSL ();
3337 }
3438
3539 public ResponseWrapper sendGet (String url , String params ,
@@ -49,19 +53,20 @@ public ResponseWrapper sendRequest(String url, String content,
4953 try {
5054 response = _sendRequest (url , content , method , authCode );
5155 break ;
52- } catch (APIConnectionException e ) {
56+ } catch (SocketTimeoutException e ) { // connect timed out
5357 if (retryTimes >= _maxRetryTimes ) {
5458 throw new APIConnectionException (e );
5559 } else {
56- LOG .debug ("Retry again - " + (retryTimes + 1 ));
60+ LOG .debug ("connect timed out - retry again - " + (retryTimes + 1 ));
5761 }
5862 }
5963 }
6064 return response ;
6165 }
6266
6367 private ResponseWrapper _sendRequest (String url , String content ,
64- RequestMethod method , String authCode ) throws APIConnectionException , APIRequestException {
68+ RequestMethod method , String authCode ) throws APIConnectionException , APIRequestException ,
69+ SocketTimeoutException {
6570 LOG .debug ("Send request to - " + url );
6671 if (null != content ) {
6772 LOG .debug ("Request Content - " + content );
@@ -73,8 +78,6 @@ private ResponseWrapper _sendRequest(String url, String content,
7378 ResponseWrapper wrapper = new ResponseWrapper ();
7479
7580 try {
76- initSSL ();
77-
7881 URL aUrl = new URL (url );
7982 conn = (HttpURLConnection ) aUrl .openConnection ();
8083 conn .setConnectTimeout (DEFAULT_CONNECTION_TIMEOUT );
@@ -165,6 +168,13 @@ private ResponseWrapper _sendRequest(String url, String content,
165168 throw new APIRequestException (wrapper );
166169 }
167170
171+ } catch (SocketTimeoutException e ) {
172+ if (e .getMessage ().contains ("connect timed out" )) {
173+ throw e ;
174+ }
175+ LOG .debug (IO_ERROR_MESSAGE , e );
176+ throw new APIConnectionException (IO_ERROR_MESSAGE , e );
177+
168178 } catch (IOException e ) {
169179 LOG .debug (IO_ERROR_MESSAGE , e );
170180 throw new APIConnectionException (IO_ERROR_MESSAGE , e );
@@ -186,16 +196,17 @@ private ResponseWrapper _sendRequest(String url, String content,
186196 }
187197
188198 protected void initSSL () {
199+ TrustManager [] tmCerts = new javax .net .ssl .TrustManager [1 ];
200+ tmCerts [0 ] = new SimpleTrustManager ();
189201 try {
190- TrustManager [] tmCerts = new javax .net .ssl .TrustManager [1 ];
191- tmCerts [0 ] = new SimpleTrustManager ();
192- javax .net .ssl .SSLContext sc = javax .net .ssl .SSLContext .getInstance ("SSL" );
193- sc .init (null , tmCerts , null );
194- javax .net .ssl .HttpsURLConnection .setDefaultSSLSocketFactory (sc .getSocketFactory ());
195- HostnameVerifier hv = new SimpleHostnameVerifier ();
196- HttpsURLConnection .setDefaultHostnameVerifier (hv );
202+ SSLContext sslContext = SSLContext .getInstance ("SSL" );
203+ sslContext .init (null , tmCerts , null );
204+ HttpsURLConnection .setDefaultSSLSocketFactory (sslContext .getSocketFactory ());
205+
206+ HostnameVerifier hostnameVerifier = new SimpleHostnameVerifier ();
207+ HttpsURLConnection .setDefaultHostnameVerifier (hostnameVerifier );
197208 } catch (Exception e ) {
198- LOG .error ("" , e );
209+ LOG .error ("Init SSL error " , e );
199210 }
200211 }
201212
0 commit comments