[英]Apache HTTP Client and conditional setting of proxy
I am instantiating an Apache HTTP Components HttpClient using the following code: 我正在使用以下代码实例化Apache HTTP组件HttpClient:
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
. 但是我只想在属性(例如
useProxy
)设置为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? 我可以根据属性值使用一对
if-then-else
块,但我想知道是否有更好的方法来实现? My goal is to externalize the control of whether or not to use a proxy, using a configuration file property or via JAVA_OPTS
. 我的目标是通过配置文件属性或通过
JAVA_OPTS
来外部化对是否使用代理的控制。
How about: 怎么样:
HttpClientBuilder builder = HttpClients.custom()
.disableConnectionState()
.disableCookieManagement();
if( useProxy )
builder = builder.setProxy(new HttpHost(proxyServerAddress, proxyServerPort));
CloseableHttpClient httpClient = builder.build();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.