0

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 (e.g. 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.

1 Answer 1

1

How about:

HttpClientBuilder builder = HttpClients.custom()
        .disableConnectionState()
        .disableCookieManagement();

if( useProxy )
    builder = builder.setProxy(new HttpHost(proxyServerAddress, proxyServerPort));

CloseableHttpClient httpClient = builder.build();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.