Java Apache HTTP客户端和代理的条件设置
我正在使用以下代码实例化Apache HTTP组件HttpClient:Java Apache HTTP客户端和代理的条件设置,java,apache-httpclient-4.x,Java,Apache Httpclient 4.x,我正在使用以下代码实例化Apache HTTP组件HttpClient: CloseableHttpClient httpClient = HttpClients.custom() .setProxy(new HttpHost(proxyServerAddress, proxyServerPort)) .disableConnectionState() .disableCookieManagement() .build(); 但我
CloseableHttpClient httpClient = HttpClients.custom()
.setProxy(new HttpHost(proxyServerAddress, proxyServerPort))
.disableConnectionState()
.disableCookieManagement()
.build();
但我只想在属性(例如useProxy
)设置为true
时设置代理。我可以根据属性值使用一对if-then-else
块,但我想知道是否有更好的方法来实现这一点?我的目标是使用配置文件属性或通过JAVA\u OPTS
将是否使用代理的控制外部化,如何:
HttpClientBuilder builder = HttpClients.custom()
.disableConnectionState()
.disableCookieManagement();
if( useProxy )
builder = builder.setProxy(new HttpHost(proxyServerAddress, proxyServerPort));
CloseableHttpClient httpClient = builder.build();