[英]Apache HTTP Client and conditional setting of proxy
我正在使用以下代码实例化Apache HTTP组件HttpClient:
CloseableHttpClient httpClient = HttpClients.custom()
.setProxy(new HttpHost(proxyServerAddress, proxyServerPort))
.disableConnectionState()
.disableCookieManagement()
.build();
但是我只想在属性(例如useProxy
)设置为true
设置代理。 我可以根据属性值使用一对if-then-else
块,但我想知道是否有更好的方法来实现? 我的目标是通过配置文件属性或通过JAVA_OPTS
来外部化对是否使用代理的控制。
怎么样:
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.