Package org.apache.http.impl.client

Examples of org.apache.http.impl.client.CloseableHttpClient


    public static void main(String[] args)throws Exception {
        Registry<ConnectionSocketFactory> reg = RegistryBuilder.<ConnectionSocketFactory>create()
                .register("http", new MyConnectionSocketFactory())
                .build();
        PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(reg);
        CloseableHttpClient httpclient = HttpClients.custom()
                .setConnectionManager(cm)
                .build();
        try {
            InetSocketAddress socksaddr = new InetSocketAddress("mysockshost", 1234);
            HttpClientContext context = HttpClientContext.create();
            context.setAttribute("socks.address", socksaddr);

            HttpHost target = new HttpHost("localhost", 80, "http");
            HttpGet request = new HttpGet("/");

            System.out.println("Executing request " + request + " to " + target + " via SOCKS proxy " + socksaddr);
            CloseableHttpResponse response = httpclient.execute(target, request, context);
            try {
                System.out.println("----------------------------------------");
                System.out.println(response.getStatusLine());
                EntityUtils.consume(response.getEntity());
            } finally {
                response.close();
            }
        } finally {
            httpclient.close();
        }
    }
View Full Code Here


        HttpClientUtils.closeQuietly(response);
    }

    @Test
    public void testCloseQuietlyHttpClientNull() throws Exception {
        final CloseableHttpClient httpclient = null;
        HttpClientUtils.closeQuietly(httpclient);
    }
View Full Code Here

        HttpClientUtils.closeQuietly(httpclient);
    }

    @Test
    public void testCloseQuietlyHttpClient() throws Exception {
        final CloseableHttpClient httpclient = Mockito.mock(CloseableHttpClient.class);
        HttpClientUtils.closeQuietly(httpclient);
        Mockito.verify(httpclient).close();
    }
View Full Code Here

        Mockito.verify(httpclient).close();
    }

    @Test
    public void testCloseQuietlyCloseableHttpClientIgnoreIOError() throws Exception {
        final CloseableHttpClient httpclient = Mockito.mock(CloseableHttpClient.class);
        Mockito.doThrow(new IOException()).when(httpclient).close();
        HttpClientUtils.closeQuietly(httpclient);
    }
View Full Code Here

        final CountDownLatch connLatch = new CountDownLatch(1);
        final CountDownLatch awaitLatch = new CountDownLatch(1);
        final ConMan conMan = new ConMan(connLatch, awaitLatch);
        final AtomicReference<Throwable> throwableRef = new AtomicReference<Throwable>();
        final CountDownLatch getLatch = new CountDownLatch(1);
        final CloseableHttpClient client = HttpClients.custom().setConnectionManager(conMan).build();
        final HttpContext context = new BasicHttpContext();
        final HttpGet httpget = new HttpGet("http://www.example.com/a");
        this.httpclient = client;

        new Thread(new Runnable() {
            public void run() {
                try {
                    client.execute(httpget, context);
                } catch(final Throwable t) {
                    throwableRef.set(t);
                } finally {
                    getLatch.countDown();
                }
View Full Code Here

        final CountDownLatch releaseLatch = new CountDownLatch(1);
        final PoolingHttpClientConnectionManager conMan = new PoolingHttpClientConnectionManager();
        final AtomicReference<Throwable> throwableRef = new AtomicReference<Throwable>();
        final CountDownLatch getLatch = new CountDownLatch(1);
        final CloseableHttpClient client = HttpClients.custom().setConnectionManager(conMan).build();
        final HttpContext context = new BasicHttpContext();
        final HttpGet httpget = new CustomGet("a", releaseLatch);
        this.httpclient = client;

        new Thread(new Runnable() {
            public void run() {
                try {
                    client.execute(getServerHttp(), httpget, context);
                } catch(final Throwable t) {
                    throwableRef.set(t);
                } finally {
                    getLatch.countDown();
                }
View Full Code Here

        final PoolingHttpClientConnectionManager conMan = new PoolingHttpClientConnectionManager();
        final AtomicReference<Throwable> throwableRef = new AtomicReference<Throwable>();
        final CountDownLatch getLatch = new CountDownLatch(1);
        final CountDownLatch startLatch = new CountDownLatch(1);
        final CloseableHttpClient client = HttpClients.custom().setConnectionManager(conMan).build();
        final HttpContext context = new BasicHttpContext();
        final HttpGet httpget = new HttpGet("a");
        this.httpclient = client;

        new Thread(new Runnable() {
            public void run() {
                try {
                    try {
                        if(!startLatch.await(1, TimeUnit.SECONDS)) {
                            throw new RuntimeException("Took too long to start!");
                        }
                    } catch(final InterruptedException interrupted) {
                        throw new RuntimeException("Never started!", interrupted);
                    }
                    client.execute(getServerHttp(), httpget, context);
                } catch(final Throwable t) {
                    throwableRef.set(t);
                } finally {
                    getLatch.countDown();
                }
View Full Code Here

        final CountDownLatch connLatch = new CountDownLatch(1);
        final CountDownLatch awaitLatch = new CountDownLatch(1);
        final ConnMan4 conMan = new ConnMan4(connLatch, awaitLatch);
        final AtomicReference<Throwable> throwableRef = new AtomicReference<Throwable>();
        final CountDownLatch getLatch = new CountDownLatch(1);
        final CloseableHttpClient client = HttpClients.custom().setConnectionManager(conMan).build();
        final HttpContext context = new BasicHttpContext();
        final HttpGet httpget = new HttpGet("a");
        this.httpclient = client;

        new Thread(new Runnable() {
            public void run() {
                try {
                    final HttpHost host = new HttpHost("127.0.0.1", port);
                    client.execute(host, httpget, context);
                } catch(final Throwable t) {
                    throwableRef.set(t);
                } finally {
                    getLatch.countDown();
                }
View Full Code Here

    public static void main(String[] args) throws Exception {
        if (args.length != 1)  {
            System.out.println("File path not given");
            System.exit(1);
        }
        CloseableHttpClient httpclient = HttpClients.createDefault();
        try {
            HttpPost httppost = new HttpPost("http://localhost:8080" +
                    "/servlets-examples/servlet/RequestInfoExample");

            FileBody bin = new FileBody(new File(args[0]));
            StringBody comment = new StringBody("A binary file of some kind", ContentType.TEXT_PLAIN);

            HttpEntity reqEntity = MultipartEntityBuilder.create()
                    .addPart("bin", bin)
                    .addPart("comment", comment)
                    .build();


            httppost.setEntity(reqEntity);

            System.out.println("executing request " + httppost.getRequestLine());
            CloseableHttpResponse response = httpclient.execute(httppost);
            try {
                System.out.println("----------------------------------------");
                System.out.println(response.getStatusLine());
                HttpEntity resEntity = response.getEntity();
                if (resEntity != null) {
                    System.out.println("Response content length: " + resEntity.getContentLength());
                }
                EntityUtils.consume(resEntity);
            } finally {
                response.close();
            }
        } finally {
            httpclient.close();
        }
    }
View Full Code Here

import org.apache.http.util.EntityUtils;

public class QuickStart {

    public static void main(String[] args) throws Exception {
        CloseableHttpClient httpclient = HttpClients.createDefault();
        try {
            HttpGet httpGet = new HttpGet("http://targethost/homepage");
            CloseableHttpResponse response1 = httpclient.execute(httpGet);
            // The underlying HTTP connection is still held by the response object
            // to allow the response content to be streamed directly from the network socket.
            // In order to ensure correct deallocation of system resources
            // the user MUST call CloseableHttpResponse#close() from a finally clause.
            // Please note that if response content is not fully consumed the underlying
            // connection cannot be safely re-used and will be shut down and discarded
            // by the connection manager.
            try {
                System.out.println(response1.getStatusLine());
                HttpEntity entity1 = response1.getEntity();
                // do something useful with the response body
                // and ensure it is fully consumed
                EntityUtils.consume(entity1);
            } finally {
                response1.close();
            }

            HttpPost httpPost = new HttpPost("http://targethost/login");
            List <NameValuePair> nvps = new ArrayList <NameValuePair>();
            nvps.add(new BasicNameValuePair("username", "vip"));
            nvps.add(new BasicNameValuePair("password", "secret"));
            httpPost.setEntity(new UrlEncodedFormEntity(nvps));
            CloseableHttpResponse response2 = httpclient.execute(httpPost);

            try {
                System.out.println(response2.getStatusLine());
                HttpEntity entity2 = response2.getEntity();
                // do something useful with the response body
                // and ensure it is fully consumed
                EntityUtils.consume(entity2);
            } finally {
                response2.close();
            }
        } finally {
            httpclient.close();
        }
    }
View Full Code Here

TOP

Related Classes of org.apache.http.impl.client.CloseableHttpClient

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.