11package org .baeldung .client ;
22
3- import org .apache .http .HttpHost ;
4- import org .apache .http .client . HttpClient ;
5- import org .apache .http .client .params . ClientPNames ;
6- import org .apache .http .params . CoreConnectionPNames ;
7- import org .apache .http .params . HttpConnectionParams ;
8- import org .apache .http .params . HttpParams ;
3+ import org .apache .http .auth . AuthScope ;
4+ import org .apache .http .auth . UsernamePasswordCredentials ;
5+ import org .apache .http .client .config . RequestConfig ;
6+ import org .apache .http .impl . client . BasicCredentialsProvider ;
7+ import org .apache .http .impl . client . CloseableHttpClient ;
8+ import org .apache .http .impl . client . HttpClientBuilder ;
99import org .springframework .beans .factory .FactoryBean ;
1010import org .springframework .beans .factory .InitializingBean ;
11+ import org .springframework .http .client .ClientHttpRequestFactory ;
12+ import org .springframework .http .client .HttpComponentsClientHttpRequestFactory ;
1113import org .springframework .stereotype .Component ;
1214import org .springframework .web .client .RestTemplate ;
1315
@@ -38,27 +40,16 @@ public boolean isSingleton() {
3840
3941 @ Override
4042 public void afterPropertiesSet () {
41- final HttpHost host = new HttpHost ("localhost" , 8080 , "http" );
42- final HttpComponentsClientHttpRequestFactoryBasicAuth requestFactory = new HttpComponentsClientHttpRequestFactoryBasicAuth (host );
43- restTemplate = new RestTemplate (requestFactory );
44-
4543 final int timeout = 5 ;
46- final HttpClient httpClient = requestFactory .getHttpClient ();
47- // - note: timeout via raw String parameters
48- // httpClient.getParams().setParameter("http.connection.timeout", timeout * 1000);
49- // httpClient.getParams().setParameter("http.socket.timeout", timeout * 1000);
5044
51- // httpClient.getParams().setParameter("http.connection-manager.timeout", new Long(timeout * 1000));
52- // httpClient.getParams().setParameter("http.protocol.head-body-timeout", timeout * 1000);
45+ final RequestConfig config = RequestConfig .custom ().setConnectTimeout (timeout * 1000 ).setConnectionRequestTimeout (timeout * 1000 ).setSocketTimeout (timeout * 1000 ).build ();
5346
54- // - note: timeout via the API
55- final HttpParams httpParams = httpClient .getParams ();
56- httpParams .setParameter (CoreConnectionPNames .CONNECTION_TIMEOUT , timeout * 1000 );
57- httpParams .setParameter (CoreConnectionPNames .SO_TIMEOUT , timeout * 1000 );
58- httpParams .setParameter (ClientPNames .CONN_MANAGER_TIMEOUT , new Long (timeout * 1000 ));
47+ final BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider ();
48+ credentialsProvider .setCredentials (new AuthScope ("localhost" , 8080 , AuthScope .ANY_REALM ), new UsernamePasswordCredentials ("user1" , "user1Pass" ));
49+ final CloseableHttpClient client = HttpClientBuilder .create ().setDefaultRequestConfig (config ).setDefaultCredentialsProvider (credentialsProvider ).build ();
5950
60- HttpConnectionParams . setConnectionTimeout ( httpParams , timeout * 1000 ); // http.connection.timeout
61- HttpConnectionParams . setSoTimeout ( httpParams , timeout * 1000 ); // http.socket.timeout
51+ final ClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory ( client );
52+ restTemplate = new RestTemplate ( requestFactory );
6253 }
6354
6455}
0 commit comments