|
| 1 | + ------ |
| 2 | + Async Http Client - Configuring a Proxy |
| 3 | + ------ |
| 4 | + Jeanfrancois Arcand |
| 5 | + ------ |
| 6 | + 2012 |
| 7 | + |
| 8 | +Configuring a Proxy |
| 9 | + |
| 10 | + The AsyncHttpClient library supports proxy, proxy authentication and proxy tunneling. |
| 11 | + Just need to create a <<<ProxyServer>>> instance: |
| 12 | + |
| 13 | ++-----+ |
| 14 | +AsyncHttpClient client = new AsyncHttpClient(); |
| 15 | +Future<Response> f = client.prepareGet("http://....) |
| 16 | + .setProxyServer(new ProxyServer("127.0.0.1", 8080)) |
| 17 | + .execute(); |
| 18 | ++-----+ |
| 19 | + |
| 20 | + If you need to use an SSL tunnel, all you need to do is: |
| 21 | + |
| 22 | ++-----+ |
| 23 | +ProxyServer ps = new ProxyServer(ProxyServer.Protocol.HTTPS, "127.0.0.1", 8080); |
| 24 | +AsyncHttpClient asyncHttpClient = new AsyncHttpClient(); |
| 25 | +RequestBuilder rb = new RequestBuilder("GET") |
| 26 | + .setProxyServer(ps) |
| 27 | + .setUrl("https://twitpic.com:443"); |
| 28 | +Future responseFuture = asyncHttpClient.executeRequest(rb.build(), new AsyncCompletionHandlerBase() { |
| 29 | + @Override |
| 30 | + public void onThrowable(Throwable t) {} |
| 31 | + |
| 32 | + @Override |
| 33 | + public Response onCompleted(Response response) throws Exception { |
| 34 | + return response; |
| 35 | + } |
| 36 | + |
| 37 | +}); |
| 38 | +Response r = responseFuture.get(); |
| 39 | ++-----+ |
| 40 | + |
| 41 | + You can also set the authentication token on the <<<ProxyServer>>> instance: |
| 42 | + |
| 43 | ++-----+ |
| 44 | +ProxyServer ps = new ProxyServer(ProxyServer.Protocol.HTTPS, "127.0.0.1", 8080, "admin", "password"); |
| 45 | +AsyncHttpClient asyncHttpClient = new AsyncHttpClient(); |
| 46 | +RequestBuilder rb = new RequestBuilder("GET") |
| 47 | + .setProxyServer(ps) |
| 48 | + .setUrl("https://twitpic.com:443"); |
| 49 | +Future responseFuture = asyncHttpClient.executeRequest(rb.build(), new AsyncCompletionHandlerBase() { |
| 50 | + @Override |
| 51 | + public void onThrowable(Throwable t) {} |
| 52 | + |
| 53 | + @Override |
| 54 | + public Response onCompleted(Response response) throws Exception { |
| 55 | + return response; |
| 56 | + } |
| 57 | + |
| 58 | +}); |
| 59 | +Response r = responseFuture.get(); |
| 60 | ++-----+ |
| 61 | + |
| 62 | + You can also set the <<<ProxyServer>>> at the <<<AsyncHttpClientConfig>>> level. In that case, all request will share |
| 63 | + the same proxy information. |
0 commit comments