在 AsyncHttpClient 2.x 中配置显式代理
在构建客户端之前,在 AsyncHttpClient 2.12.4 配置中设置 ProxyServer,并就 JVM 属性发现做出明确选择。
DefaultAsyncHttpClientConfig.Builder config = Dsl.config()
.setProxyServer(
new ProxyServer.Builder("proxy.example", 8080))
.setRequestTimeout(15000);
try (AsyncHttpClient client = Dsl.asyncHttpClient(config)) {
Response response = client.prepareGet("https://www.example.com/")
.execute().toCompletableFuture().join();
System.out.println(response.getStatusCode());
}
这是 org.asynchttpclient 2.12.4 API,ProxyServer 位于其 proxy 包中。对于这个公开的 HTTPS 示例,请把中间层替换为 HTTP 代理列表中未经认证的候选,并单独设置网站。配置在构建客户端之前提供,15 秒的请求预算也是该配置的一部分。
示例在客户端的生命周期内等待 future。异步应用也可以改为组合它,但必须保留客户端,直到分配给它的工作完成。
如果要以受支持的 JVM 属性作为策略来源,请显式选择 setUseProxyProperties(true)。某个属性存在于一个运行时中,并不能证明每个客户端都启用了它。
较早的 Dispatch 示例通常包装 com.ning 并使用另一个启用属性。请与底层客户端的 imports 和 API 保持一致,而不要把那种配置与这个 2.x builder 混用。