3
3
import java .security .KeyStore ;
4
4
5
5
import org .apache .http .HttpVersion ;
6
+ import org .apache .http .client .HttpClient ;
6
7
import org .apache .http .client .params .ClientPNames ;
7
8
import org .apache .http .conn .ClientConnectionManager ;
8
9
import org .apache .http .conn .scheme .PlainSocketFactory ;
22
23
@ SuppressWarnings ("deprecation" )
23
24
public class PortalHttpClient {
24
25
25
- // public static HttpClient getNewHttpClient2() {
26
- // try {
27
- // KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
28
- // trustStore.load(null, null);
29
- //
30
- // MySSLSocketFactory sf = new MySSLSocketFactory(trustStore);
31
- // sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
32
- //
33
- // HttpParams params = new BasicHttpParams();
34
- // HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
35
- // HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);
36
- //
37
- // SchemeRegistry registry = new SchemeRegistry();
38
- // registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
39
- // registry.register(new Scheme("https", sf, 443));
40
- //
41
- // ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, registry);
42
- //
43
- // DefaultHttpClient httpClient = new DefaultHttpClient(ccm, params);
44
- //
45
- // int timeout = 40; // seconds
46
- // HttpParams httpParams = httpClient.getParams();
47
- // httpParams.setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, timeout * 1000);
48
- // httpParams.setParameter(CoreConnectionPNames.SO_TIMEOUT, timeout * 1000);
49
- // httpParams.setParameter(ClientPNames.CONN_MANAGER_TIMEOUT, new Long(timeout * 1000));
50
- // return httpClient;
51
- // } catch (Exception e) {
52
- // return new DefaultHttpClient();
53
- // }
54
- // }
55
-
56
- public static CloseableHttpClient getNewHttpClient () {
26
+ public static HttpClient getNewHttpClient () {
57
27
try {
58
28
KeyStore trustStore = KeyStore .getInstance (KeyStore .getDefaultType ());
59
29
trustStore .load (null , null );
@@ -70,18 +40,52 @@ public static CloseableHttpClient getNewHttpClient() {
70
40
registry .register (new Scheme ("https" , sf , 443 ));
71
41
72
42
ClientConnectionManager ccm = new ThreadSafeClientConnManager (params , registry );
43
+ try (DefaultHttpClient httpClient = new DefaultHttpClient (ccm , params )) {
44
+ int timeout = 40 ; // seconds
45
+ HttpParams httpParams = httpClient .getParams ();
46
+ httpParams .setParameter (CoreConnectionPNames .CONNECTION_TIMEOUT , timeout * 1000 );
47
+ httpParams .setParameter (CoreConnectionPNames .SO_TIMEOUT , timeout * 1000 );
48
+ httpParams .setParameter (ClientPNames .CONN_MANAGER_TIMEOUT , new Long (timeout * 1000 ));
49
+ return httpClient ;
50
+ }
51
+ } catch (Exception e ) {
52
+ return new DefaultHttpClient ();
53
+ }
54
+ }
55
+
56
+ public static HttpClient getCloseableHttpClient () {
57
+ try {
58
+ // Initialize truststore and custom socket factory (sf)
59
+ KeyStore trustStore = KeyStore .getInstance (KeyStore .getDefaultType ());
60
+ trustStore .load (null , null );
61
+ MySSLSocketFactory sf = new MySSLSocketFactory (trustStore );
62
+ sf .setHostnameVerifier (SSLSocketFactory .ALLOW_ALL_HOSTNAME_VERIFIER );
73
63
74
- CloseableHttpClient httpClient = new DefaultHttpClient (ccm , params );
64
+ // Create HttpParams and SchemeRegistry
65
+ HttpParams params = new BasicHttpParams ();
66
+ HttpProtocolParams .setVersion (params , HttpVersion .HTTP_1_1 );
67
+ HttpProtocolParams .setContentCharset (params , HTTP .UTF_8 );
68
+
69
+ SchemeRegistry registry = new SchemeRegistry ();
70
+ registry .register (new Scheme ("http" , PlainSocketFactory .getSocketFactory (), 80 ));
71
+ registry .register (new Scheme ("https" , sf , 443 ));
72
+
73
+ // Initialize ClientConnectionManager
74
+ ClientConnectionManager ccm = new ThreadSafeClientConnManager (params , registry );
75
75
76
- int timeout = 40 ; // seconds
77
- HttpParams httpParams = httpClient .getParams ();
78
- httpParams .setParameter (CoreConnectionPNames .CONNECTION_TIMEOUT , timeout * 1000 );
79
- httpParams .setParameter (CoreConnectionPNames .SO_TIMEOUT , timeout * 1000 );
80
- httpParams .setParameter (ClientPNames .CONN_MANAGER_TIMEOUT , (long ) timeout * 1000 );
76
+ // Use try-with-resources to ensure httpClient is closed after use
77
+ try (CloseableHttpClient httpClient = new DefaultHttpClient (ccm , params )) {
78
+ int timeout = 40 ; // seconds
79
+ HttpParams httpParams = httpClient .getParams ();
80
+ httpParams .setParameter (CoreConnectionPNames .CONNECTION_TIMEOUT , timeout * 1000 );
81
+ httpParams .setParameter (CoreConnectionPNames .SO_TIMEOUT , timeout * 1000 );
82
+ httpParams .setParameter (ClientPNames .CONN_MANAGER_TIMEOUT , (long ) timeout * 1000 );
81
83
82
- return httpClient ;
84
+ return httpClient ;
85
+ }
83
86
} catch (Exception e ) {
84
- return HttpClients .createDefault (); // Returns a default CloseableHttpClient instance
87
+ e .printStackTrace ();
88
+ return HttpClients .createDefault (); // Fallback to default HttpClient
85
89
}
86
90
}
87
91
}
0 commit comments