Example usage for org.apache.http.impl.client CloseableHttpClient close

List of usage examples for org.apache.http.impl.client CloseableHttpClient close

Introduction

In this page you can find the example usage for org.apache.http.impl.client CloseableHttpClient close.

Prototype

public void close() throws IOException;

Source Link

Document

Closes this stream and releases any system resources associated with it.

Usage

From source file:com.sim.demo.httpclient.httpget.BaiduDemo.java

/**
 * @param args//from  w ww  . j  av  a2  s  . c o m
 * @throws IOException 
 * @throws ClientProtocolException 
 */
public static void main(String[] args) throws ClientProtocolException, IOException {
    CloseableHttpClient httpClient = HttpClients.createDefault();

    HttpGet httpGet = new HttpGet("http://www.baidu.com");

    System.out.println(httpGet.getURI().toString());

    CloseableHttpResponse response = httpClient.execute(httpGet);

    int statusCode = response.getStatusLine().getStatusCode();
    if (statusCode == 200) {
        HttpEntity entity = response.getEntity();

        String context = EntityUtils.toString(entity, "UTF-8");

        System.out.println("context : " + context);
    }

    httpClient.close();

}

From source file:ClientAuthentication.java

public static void main(String[] args) throws Exception {
    CredentialsProvider credsProvider = new BasicCredentialsProvider();
    credsProvider.setCredentials(new AuthScope("localhost", 443),
            new UsernamePasswordCredentials("username", "password"));
    CloseableHttpClient httpclient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();
    try {//w  w  w . j a  va  2s. c o  m
        HttpGet httpget = new HttpGet("http://localhost/");

        System.out.println("Executing request " + httpget.getRequestLine());
        CloseableHttpResponse response = httpclient.execute(httpget);
        try {
            System.out.println("----------------------------------------");
            System.out.println(response.getStatusLine());
            EntityUtils.consume(response.getEntity());
        } finally {
            response.close();
        }
    } finally {
        httpclient.close();
    }
}

From source file:com.lxf.spider.client.ClientAbortMethod.java

public final static void main(String[] args) throws Exception {
    CloseableHttpClient httpclient = HttpClients.createDefault();
    try {//from  w ww.  j av  a 2  s.c  om
        HttpGet httpget = new HttpGet("http://www.apache.org/");

        System.out.println("Executing request " + httpget.getURI());
        CloseableHttpResponse response = httpclient.execute(httpget);
        try {
            System.out.println("----------------------------------------");
            System.out.println(response.getStatusLine());
            // Do not feel like reading the response body
            // Call abort on the request object
            httpget.abort();
        } finally {
            response.close();
        }
    } finally {
        httpclient.close();
    }
}

From source file:demo.example.ClientAbortMethod.java

public final static void main(String[] args) throws Exception {
    CloseableHttpClient httpclient = HttpClients.createDefault();
    try {//from  ww  w . ja  va 2 s . c  o m
        HttpGet httpget = new HttpGet("http://httpbin.org/get");

        System.out.println("Executing request " + httpget.getURI());
        CloseableHttpResponse response = httpclient.execute(httpget);
        try {
            System.out.println("----------------------------------------");
            System.out.println(response.getStatusLine());
            // Do not feel like reading the response body
            // Call abort on the request object
            httpget.abort();
        } finally {
            response.close();
        }
    } finally {
        httpclient.close();
    }
}

From source file:cn.anthony.util.ClientAuthentication.java

public static void main(String[] args) throws Exception {
    CredentialsProvider credsProvider = new BasicCredentialsProvider();
    credsProvider.setCredentials(new AuthScope("httpbin.org", 80),
            new UsernamePasswordCredentials("user", "passwd"));
    CloseableHttpClient httpclient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();
    try {//ww  w .  j  a  v a2  s .  co m
        HttpGet httpget = new HttpGet("http://httpbin.org/basic-auth/user/passwd");

        System.out.println("Executing request " + httpget.getRequestLine());
        CloseableHttpResponse response = httpclient.execute(httpget);
        try {
            System.out.println("----------------------------------------");
            System.out.println(response.getStatusLine());
            System.out.println(EntityUtils.toString(response.getEntity()));
        } finally {
            response.close();
        }
    } finally {
        httpclient.close();
    }
}

From source file:cn.anthony.util.ClientCustomSSL.java

public final static void main(String[] args) throws Exception {
    // Trust own CA and all self-signed certs
    SSLContext sslcontext = SSLContexts.custom().loadTrustMaterial(new File("my.keystore"),
            "nopassword".toCharArray(), new TrustSelfSignedStrategy()).build();
    // Allow TLSv1 protocol only
    SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext, new String[] { "TLSv1" },
            null, SSLConnectionSocketFactory.getDefaultHostnameVerifier());
    CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).build();
    try {/* ww w  .j ava2s . c o  m*/

        HttpGet httpget = new HttpGet("https://httpbin.org/");

        System.out.println("Executing request " + httpget.getRequestLine());

        CloseableHttpResponse response = httpclient.execute(httpget);
        try {
            HttpEntity entity = response.getEntity();

            System.out.println("----------------------------------------");
            System.out.println(response.getStatusLine());
            EntityUtils.consume(entity);
        } finally {
            response.close();
        }
    } finally {
        httpclient.close();
    }
}

From source file:com.zhch.example.commons.http.v4_5.ClientCustomSSL.java

public final static void main(String[] args) throws Exception {
    // Trust own CA and all self-signed certs
    SSLContext sslcontext = SSLContexts.custom()
            .loadTrustMaterial(new File("d:\\workspace_mars\\resume_import_system\\jssecacerts"), null,
                    new TrustSelfSignedStrategy())
            .build();//w w w  .  j  a va  2s  .  c  o m
    // Allow TLSv1 protocol only
    SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext, new String[] { "TLSv1" },
            null, SSLConnectionSocketFactory.getDefaultHostnameVerifier());
    CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).build();
    try {

        HttpGet httpget = new HttpGet("https://passport.zhaopin.com/org/login");

        System.out.println("Executing request " + httpget.getRequestLine());

        CloseableHttpResponse response = httpclient.execute(httpget);
        try {
            HttpEntity entity = response.getEntity();

            System.out.println("----------------------------------------");
            System.out.println(response.getStatusLine());
            EntityUtils.consume(entity);
        } finally {
            response.close();
        }
    } finally {
        httpclient.close();
    }
}

From source file:ddu.core.httpclient.ClientCustomSSL.java

public final static void main(String[] args) throws Exception {
    // Trust own CA and all self-signed certs
    KeyStore trustKeyStore = KeyStore.getInstance("JKS");

    // get user password and file input stream
    char[] password = "123456".toCharArray();
    java.io.FileInputStream fis = null;
    try {/*from  w ww .  ja  va 2  s.  c  o m*/
        fis = new java.io.FileInputStream("keyStoreName");
        trustKeyStore.load(fis, password);
    } finally {
        if (fis != null) {
            fis.close();
        }
    }

    SSLContext sslcontext = SSLContexts.custom().loadTrustMaterial(trustKeyStore, new TrustSelfSignedStrategy())
            .build();
    // Allow TLSv1 protocol only
    SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext, new String[] { "TLSv1" },
            null, SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);

    CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).build();
    try {

        HttpGet httpget = new HttpGet("https://httpbin.org/");

        System.out.println("Executing request " + httpget.getRequestLine());

        CloseableHttpResponse response = httpclient.execute(httpget);
        try {
            HttpEntity entity = response.getEntity();

            System.out.println("----------------------------------------");
            System.out.println(response.getStatusLine());
            EntityUtils.consume(entity);
        } finally {
            response.close();
        }
    } finally {
        httpclient.close();
    }
}

From source file:demo.example.ClientWinAuth.java

public final static void main(String[] args) throws Exception {

    if (!WinHttpClients.isWinAuthAvailable()) {
        System.out.println("Integrated Win auth is not supported!!!");
    }/*from ww w . j  a  v  a 2  s.  c  o m*/

    CloseableHttpClient httpclient = WinHttpClients.createDefault();
    // There is no need to provide user credentials
    // HttpClient will attempt to access current user security context through
    // Windows platform specific methods via JNI.
    try {
        HttpGet httpget = new HttpGet("http://winhost/");

        System.out.println("Executing request " + httpget.getRequestLine());
        CloseableHttpResponse response = httpclient.execute(httpget);
        try {
            System.out.println("----------------------------------------");
            System.out.println(response.getStatusLine());
            EntityUtils.consume(response.getEntity());
        } finally {
            response.close();
        }
    } finally {
        httpclient.close();
    }
}

From source file:interoperabilite.webservice.client.ClientCustomPublicSuffixList.java

public final static void main(String[] args) throws Exception {

    // Use PublicSuffixMatcherLoader to load public suffix list from a file,
    // resource or from an arbitrary URL
    PublicSuffixMatcher publicSuffixMatcher = PublicSuffixMatcherLoader
            .load(new URL("https://publicsuffix.org/list/effective_tld_names.dat"));

    // Please use the publicsuffix.org URL to download the list no more than once per day !!!
    // Please consider making a local copy !!!

    DefaultHostnameVerifier hostnameVerifier = new DefaultHostnameVerifier(publicSuffixMatcher);

    RFC6265CookieSpecProvider cookieSpecProvider = new RFC6265CookieSpecProvider(publicSuffixMatcher);
    Lookup<CookieSpecProvider> cookieSpecRegistry = RegistryBuilder.<CookieSpecProvider>create()
            .register(CookieSpecs.DEFAULT, cookieSpecProvider)
            .register(CookieSpecs.STANDARD, cookieSpecProvider)
            .register(CookieSpecs.STANDARD_STRICT, cookieSpecProvider).build();

    CloseableHttpClient httpclient = HttpClients.custom().setSSLHostnameVerifier(hostnameVerifier)
            .setDefaultCookieSpecRegistry(cookieSpecRegistry).build();
    try {//from w  ww  . j av a2  s  . co  m

        HttpGet httpget = new HttpGet("https://httpbin.org/");

        System.out.println("executing request " + httpget.getRequestLine());

        CloseableHttpResponse response = httpclient.execute(httpget);
        try {
            HttpEntity entity = response.getEntity();

            System.out.println("----------------------------------------");
            System.out.println(response.getStatusLine());
            EntityUtils.consume(entity);
        } finally {
            response.close();
        }
    } finally {
        httpclient.close();
    }
}