List of usage examples for org.apache.http.impl.nio.client HttpAsyncClients createDefault
public static CloseableHttpAsyncClient createDefault()
From source file:rx.apache.http.examples.ExampleObservableHttp.java
public static void main(String args[]) { CloseableHttpAsyncClient httpclient = HttpAsyncClients.createDefault(); // final RequestConfig requestConfig = RequestConfig.custom() // .setSocketTimeout(3000) // .setConnectTimeout(3000).build(); // final CloseableHttpAsyncClient httpclient = HttpAsyncClients.custom() // .setDefaultRequestConfig(requestConfig) // .setMaxConnPerRoute(20) // .setMaxConnTotal(50) // .build(); try {// w w w. j a va 2 s . com httpclient.start(); executeViaObservableHttpWithForEach(httpclient); executeStreamingViaObservableHttpWithForEach(httpclient); } catch (Exception e) { e.printStackTrace(); } finally { try { httpclient.close(); } catch (IOException e1) { e1.printStackTrace(); } } CloseableHttpAsyncClient httpClient = HttpAsyncClients.createDefault(); ObservableHttp.createGet("http://www.wikipedia.com", httpClient).toObservable(); ObservableHttp.createRequest(HttpAsyncMethods.createGet("http://www.wikipedia.com"), httpClient) .toObservable(); }
From source file:httpasync.AsyncClientHttpExchange.java
public static void main(final String[] args) throws Exception { CloseableHttpAsyncClient httpclient = HttpAsyncClients.createDefault(); try {/*from www. j a v a 2 s .c o m*/ httpclient.start(); HttpGet request = new HttpGet("http://www.apache.org/"); Future<HttpResponse> future = httpclient.execute(request, null); HttpResponse response = future.get(); System.out.println("Response: " + response.getStatusLine()); System.out.println("Response: " + response.getEntity().getContent()); System.out.println("Shutting down"); } finally { httpclient.close(); } System.out.println("Done"); }
From source file:com.boonya.http.async.examples.nio.client.AsyncClientHttpExchange.java
public static void main(final String[] args) throws Exception { CloseableHttpAsyncClient httpclient = HttpAsyncClients.createDefault(); try {//from w w w . j a v a2s. co m httpclient.start(); HttpGet request = new HttpGet("http://www.apache.org/"); Future<HttpResponse> future = httpclient.execute(request, null); HttpResponse response = future.get(); System.out.println("Response: " + response.getStatusLine()); System.out.println("Shutting down"); } finally { httpclient.close(); } System.out.println("Done"); }
From source file:com.boonya.http.async.examples.nio.client.AsyncClientExecuteProxy.java
public static void main(String[] args) throws Exception { CloseableHttpAsyncClient httpclient = HttpAsyncClients.createDefault(); try {// www . j a v a2s . c om httpclient.start(); HttpHost proxy = new HttpHost("someproxy", 8080); RequestConfig config = RequestConfig.custom().setProxy(proxy).build(); HttpGet request = new HttpGet("https://issues.apache.org/"); request.setConfig(config); Future<HttpResponse> future = httpclient.execute(request, null); HttpResponse response = future.get(); System.out.println("Response: " + response.getStatusLine()); System.out.println("Shutting down"); } finally { httpclient.close(); } }
From source file:com.boonya.http.async.examples.nio.client.AsyncClientCustomContext.java
public final static void main(String[] args) throws Exception { CloseableHttpAsyncClient httpclient = HttpAsyncClients.createDefault(); try {/*from www .j a va 2s. co m*/ // Create a local instance of cookie store CookieStore cookieStore = new BasicCookieStore(); // Create local HTTP context HttpClientContext localContext = HttpClientContext.create(); // Bind custom cookie store to the local context localContext.setCookieStore(cookieStore); HttpGet httpget = new HttpGet("http://localhost/"); System.out.println("Executing request " + httpget.getRequestLine()); httpclient.start(); // Pass local context as a parameter Future<HttpResponse> future = httpclient.execute(httpget, localContext, null); // Please note that it may be unsafe to access HttpContext instance // while the request is still being executed HttpResponse response = future.get(); System.out.println("Response: " + response.getStatusLine()); List<Cookie> cookies = cookieStore.getCookies(); for (int i = 0; i < cookies.size(); i++) { System.out.println("Local cookie: " + cookies.get(i)); } System.out.println("Shutting down"); } finally { httpclient.close(); } }
From source file:com.boonya.http.async.examples.nio.client.AsyncClientHttpExchangeStreaming.java
public static void main(final String[] args) throws Exception { CloseableHttpAsyncClient httpclient = HttpAsyncClients.createDefault(); try {//w ww . j a va 2s . co m httpclient.start(); Future<Boolean> future = httpclient.execute(HttpAsyncMethods.createGet("http://localhost:8080/"), new MyResponseConsumer(), null); Boolean result = future.get(); if (result != null && result.booleanValue()) { System.out.println("Request successfully executed"); } else { System.out.println("Request failed"); } System.out.println("Shutting down"); } finally { httpclient.close(); } System.out.println("Done"); }
From source file:httpasync.ZeroCopyHttpExchange.java
public static void main(final String[] args) throws Exception { CloseableHttpAsyncClient httpclient = HttpAsyncClients.createDefault(); try {/*from w ww .j a va 2 s.c o m*/ httpclient.start(); File upload = new File(args[0]); File download = new File(args[1]); ZeroCopyPost httpost = new ZeroCopyPost("http://localhost:8080/", upload, ContentType.create("text/plain")); ZeroCopyConsumer<File> consumer = new ZeroCopyConsumer<File>(download) { @Override protected File process(final HttpResponse response, final File file, final ContentType contentType) throws Exception { if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) { throw new ClientProtocolException("Upload failed: " + response.getStatusLine()); } return file; } }; Future<File> future = httpclient.execute(httpost, consumer, null); File result = future.get(); System.out.println("Response file length: " + result.length()); System.out.println("Shutting down"); } finally { httpclient.close(); } System.out.println("Done"); }
From source file:com.boonya.http.async.examples.nio.client.ZeroCopyHttpExchange.java
public static void main(final String[] args) throws Exception { CloseableHttpAsyncClient httpclient = HttpAsyncClients.createDefault(); try {// w w w.j av a 2 s . c o m httpclient.start(); File upload = new File(args[0]); File download = new File(args[1]); ZeroCopyPost httpost = new ZeroCopyPost("http://localhost:8080/", upload, ContentType.create("text/plain")); ZeroCopyConsumer<File> consumer = new ZeroCopyConsumer<File>(download) { @Override protected File process(final HttpResponse response, final File file, final ContentType contentType) throws Exception { if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) { throw new ClientProtocolException("Upload failed: " + response.getStatusLine()); } return file; } }; Future<File> future = httpclient.execute(httpost, consumer, null); File result = future.get(); System.out.println("Response file length: " + result.length()); System.out.println("Shutting down"); } finally { httpclient.close(); } System.out.println("Done"); }
From source file:httpasync.QuickStart.java
public static void main(String[] args) throws Exception { CloseableHttpAsyncClient httpclient = HttpAsyncClients.createDefault(); try {// w w w. j av a2 s . c o m // Start the client httpclient.start(); // Execute request final HttpGet request1 = new HttpGet("http://www.apache.org/"); Future<HttpResponse> future = httpclient.execute(request1, null); // and wait until response is received HttpResponse response1 = future.get(); System.out.println(request1.getRequestLine() + "->" + response1.getStatusLine()); // One most likely would want to use a callback for operation result final CountDownLatch latch1 = new CountDownLatch(1); final HttpGet request2 = new HttpGet("http://www.apache.org/"); httpclient.execute(request2, new FutureCallback<HttpResponse>() { @Override public void completed(final HttpResponse response2) { latch1.countDown(); System.out.println(request2.getRequestLine() + "->" + response2.getStatusLine()); } @Override public void failed(final Exception ex) { latch1.countDown(); System.out.println(request2.getRequestLine() + "->" + ex); } @Override public void cancelled() { latch1.countDown(); System.out.println(request2.getRequestLine() + " cancelled"); } }); latch1.await(); // In real world one most likely would want also want to stream // request and response body content final CountDownLatch latch2 = new CountDownLatch(1); final HttpGet request3 = new HttpGet("http://www.apache.org/"); HttpAsyncRequestProducer producer3 = HttpAsyncMethods.create(request3); AsyncCharConsumer<HttpResponse> consumer3 = new AsyncCharConsumer<HttpResponse>() { HttpResponse response; @Override protected void onResponseReceived(final HttpResponse response) { this.response = response; } @Override protected void onCharReceived(final CharBuffer buf, final IOControl ioctrl) throws IOException { // Do something useful } @Override protected void releaseResources() { } @Override protected HttpResponse buildResult(final HttpContext context) { return this.response; } }; httpclient.execute(producer3, consumer3, new FutureCallback<HttpResponse>() { @Override public void completed(final HttpResponse response3) { latch2.countDown(); System.out.println(request2.getRequestLine() + "->" + response3.getStatusLine()); } @Override public void failed(final Exception ex) { latch2.countDown(); System.out.println(request2.getRequestLine() + "->" + ex); } @Override public void cancelled() { latch2.countDown(); System.out.println(request2.getRequestLine() + " cancelled"); } }); latch2.await(); } finally { httpclient.close(); } }
From source file:test.AsyncClientCustomContext.java
public final static void main(String[] args) throws Exception { CloseableHttpAsyncClient httpclient = HttpAsyncClients.createDefault(); HttpEntity entity = null;//from w ww. ja v a 2 s . c o m String jsonContent = ""; try { // Create a local instance of cookie store CookieStore cookieStore = new BasicCookieStore(); // Create local HTTP context HttpClientContext localContext = HttpClientContext.create(); // Bind custom cookie store to the local context localContext.setCookieStore(cookieStore); HttpGet httpget = new HttpGet("http://viphp.sinaapp.com/baidu/translate/translate.php?origin="); System.out.println("Executing request " + httpget.getRequestLine()); httpclient.start(); // Pass local context as a parameter Future<HttpResponse> future = httpclient.execute(httpget, localContext, null); // Please note that it may be unsafe to access HttpContext instance // while the request is still being executed HttpResponse response = future.get(); System.out.println("Response: " + response.getStatusLine()); entity = response.getEntity(); jsonContent = EntityUtils.toString(entity, "UTF-8"); System.out.println("jsonContent:" + jsonContent); List<Cookie> cookies = cookieStore.getCookies(); for (int i = 0; i < cookies.size(); i++) { System.out.println("Local cookie: " + cookies.get(i)); } System.out.println("Shutting down"); } finally { httpclient.close(); } }