@Test public void testGlobalProxy() throws IOException, ExecutionException, TimeoutException, InterruptedException { try (AsyncHttpClient client = asyncHttpClient(config().setProxyServer(proxyServer("localhost", port1)))) { String target = "http://localhost:1234/"; Future<Response> f = client.prepareGet(target).execute(); Response resp = f.get(3, TimeUnit.SECONDS); assertNotNull(resp); assertEquals(resp.getStatusCode(), HttpServletResponse.SC_OK); assertEquals(resp.getHeader("target"), "/"); } }
@Test public void testNonProxyHostsRequestOverridesConfig() { ProxyServer configProxy = proxyServer("localhost", port1 - 1).build(); ProxyServer requestProxy = proxyServer("localhost", port1).setNonProxyHost("localhost").build(); try (AsyncHttpClient client = asyncHttpClient(config().setProxyServer(configProxy))) { String target = "http://localhost:1234/"; client.prepareGet(target).setProxyServer(requestProxy).execute().get(); assertFalse(true); } catch (Throwable e) { assertNotNull(e.getCause()); assertEquals(e.getCause().getClass(), ConnectException.class); } }
@Test public void testBothProxies() throws IOException, ExecutionException, TimeoutException, InterruptedException { try (AsyncHttpClient client = asyncHttpClient(config().setProxyServer(proxyServer("localhost", port1 - 1)))) { String target = "http://localhost:1234/"; Future<Response> f = client.prepareGet(target).setProxyServer(proxyServer("localhost", port1)).execute(); Response resp = f.get(3, TimeUnit.SECONDS); assertNotNull(resp); assertEquals(resp.getStatusCode(), HttpServletResponse.SC_OK); assertEquals(resp.getHeader("target"), "/"); } }
proxyServer = proxyServer.setRealm(basicAuthRealm(proxyServerInfo.getPrincipal(), proxyServerInfo.getPassword())); asyncHttpClient = asyncHttpClient(config().setProxyServer(proxyServer)); } else { asyncHttpClient = asyncHttpClient();
private AsyncRequestClient(Builder builder) { this.baseUri = URI.create(builder.baseUri); DefaultAsyncHttpClientConfig.Builder clientConfigBuilder = builder.clientConfigBuilder; clientConfigBuilder.addResponseFilter(new RequestRetryFilter(builder.maxRetries, Optional.fromNullable(builder.retryPredicate))); if (Optional.fromNullable(builder.proxyServer).isPresent()) { proxyServer = Optional.fromNullable(builder.proxyServer); clientConfigBuilder.setProxyServer(proxyServer.get()); clientConfigBuilder.setRealm(proxyServer.get().getRealm()); } else { proxyServer = Optional.absent(); } this.clientConfig = clientConfigBuilder.build(); this.client = new DefaultAsyncHttpClient(clientConfig); }
try (AsyncHttpClient asyncHttpClient = asyncHttpClient(config().setProxyServer(ps).setUseInsecureTrustManager(true))) { final CountDownLatch latch = new CountDownLatch(1); final AtomicReference<String> text = new AtomicReference<>("");
public SimpleAsyncHttpClient build() { if (realmBuilder != null) { configBuilder.setRealm(realmBuilder.build()); } if (proxyHost != null) { Realm realm = null; if (proxyPrincipal != null) { AuthScheme proxyAuthScheme = withDefault(this.proxyAuthScheme, AuthScheme.BASIC); realm = realm(proxyAuthScheme, proxyPrincipal, proxyPassword).build(); } configBuilder.setProxyServer(proxyServer(proxyHost, proxyPort).setRealm(realm).build()); } configBuilder.addIOExceptionFilter(new ResumableIOExceptionFilter()); SimpleAsyncHttpClient sc = new SimpleAsyncHttpClient(configBuilder.build(), requestBuilder, defaultThrowableHandler, errorDocumentBehaviour, enableResumableDownload, ahc, listener); return sc; } }
@Test public void testDecompressBodyWithProxy() throws Exception { AsyncHttpClientConfig config = config() .setFollowRedirect(true) .setProxyServer(proxyServer("localhost", port1).build()) .setUseInsecureTrustManager(true) .build(); try (AsyncHttpClient asyncHttpClient = asyncHttpClient(config)) { String body = "hello world"; Response r = asyncHttpClient.executeRequest(post(getTargetUrl2()) .setHeader("X-COMPRESS", "true") .setBody(body)).get(); assertEquals(r.getStatusCode(), 200); assertEquals(r.getResponseBody(), body); } }
public Builder setProxyServer(ProxyServer.Builder proxyServerBuilder) { return setProxyServer(proxyServerBuilder.build()); }
@Test public void testConfigProxy() throws Exception { AsyncHttpClientConfig config = config() .setFollowRedirect(true) .setProxyServer(proxyServer("localhost", port1).build()) .setUseInsecureTrustManager(true) .build(); try (AsyncHttpClient asyncHttpClient = asyncHttpClient(config)) { Response r = asyncHttpClient.executeRequest(get(getTargetUrl2())).get(); assertEquals(r.getStatusCode(), 200); } }
@Test public void testNoDirectRequestBodyWithProxy() throws Exception { AsyncHttpClientConfig config = config() .setFollowRedirect(true) .setProxyServer(proxyServer("localhost", port1).build()) .setUseInsecureTrustManager(true) .build(); try (AsyncHttpClient asyncHttpClient = asyncHttpClient(config)) { Response r = asyncHttpClient.executeRequest(post(getTargetUrl2()).setBody(new ByteArrayBodyGenerator(LARGE_IMAGE_BYTES))).get(); assertEquals(r.getStatusCode(), 200); } }