I am instantiating an Apache HTTP Components HttpClient using the following code:
CloseableHttpClient httpClient = HttpClients.custom()
.setProxy(new HttpHost(proxyServerAddress, proxyServerPort))
.disableConnectionState()
.disableCookieManagement()
.build();
But I would like to set the proxy only if an property (eg useProxy
) is set to true
. I can use an if-then-else
pair of blocks based on the property value, but I was wondering if there is a better way to achieve this? My goal is to externalize the control of whether or not to use a proxy, using a configuration file property or via JAVA_OPTS
.
How about:
HttpClientBuilder builder = HttpClients.custom()
.disableConnectionState()
.disableCookieManagement();
if( useProxy )
builder = builder.setProxy(new HttpHost(proxyServerAddress, proxyServerPort));
CloseableHttpClient httpClient = builder.build();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.