Example usage for org.apache.http.client CookieStore getCookies

List of usage examples for org.apache.http.client CookieStore getCookies

Introduction

In this page you can find the example usage for org.apache.http.client CookieStore getCookies.

Prototype

List<Cookie> getCookies();

Source Link

Document

Returns all cookies contained in this store.

Usage

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

public final static void main(String[] args) throws Exception {
    CloseableHttpClient httpclient = HttpClients.createDefault();
    try {/*  w  ww  .j a v a 2 s.c  o  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());

        // Pass local context as a parameter
        CloseableHttpResponse response = httpclient.execute(httpget, localContext);
        try {
            System.out.println("----------------------------------------");
            System.out.println(response.getStatusLine());
            List<Cookie> cookies = cookieStore.getCookies();
            for (int i = 0; i < cookies.size(); i++) {
                System.out.println("Local cookie: " + cookies.get(i));
            }
            EntityUtils.consume(response.getEntity());
        } finally {
            response.close();
        }
    } finally {
        httpclient.close();
    }
}

From source file:demo.example.ClientCustomContext.java

public final static void main(String[] args) throws Exception {
    CloseableHttpClient httpclient = HttpClients.createDefault();
    try {//from w  w  w .  j  a v  a  2 s .c  om
        // 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://httpbin.org/cookies");
        System.out.println("Executing request " + httpget.getRequestLine());

        // Pass local context as a parameter
        CloseableHttpResponse response = httpclient.execute(httpget, localContext);
        try {
            System.out.println("----------------------------------------");
            System.out.println(response.getStatusLine());
            List<Cookie> cookies = cookieStore.getCookies();
            for (int i = 0; i < cookies.size(); i++) {
                System.out.println("Local cookie: " + cookies.get(i));
            }
            EntityUtils.consume(response.getEntity());
        } finally {
            response.close();
        }
    } finally {
        httpclient.close();
    }
}

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

public final static void main(String[] args) throws Exception {
    CloseableHttpClient httpclient = HttpClients.createDefault();
    try {/*from w ww. ja v  a2s .com*/
        // 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://httpbin.org/cookies");
        System.out.println("Executing request " + httpget.getRequestLine());

        // Pass local context as a parameter
        CloseableHttpResponse response = httpclient.execute(httpget, localContext);
        try {
            System.out.println("----------------------------------------");
            System.out.println(response.getStatusLine());
            List<Cookie> cookies = cookieStore.getCookies();
            for (int i = 0; i < cookies.size(); i++) {
                System.out.println("Local cookie: " + cookies.get(i));
            }
            EntityUtils.consume(response.getEntity());
        } finally {
            response.close();
        }
    } finally {
        httpclient.close();
    }
}

From source file:com.mama100.android.member.http.ClientCustomContext.java

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

    HttpClient httpclient = new DefaultHttpClient();

    // Create a local instance of cookie store
    CookieStore cookieStore = new BasicCookieStore();

    // Create local HTTP context
    HttpContext localContext = new BasicHttpContext();
    // Bind custom cookie store to the local context
    localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);

    HttpGet httpget = new HttpGet("http://www.weibo.com/");

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

    // Pass local context as a parameter
    HttpResponse response = httpclient.execute(httpget, localContext);
    HttpEntity entity = response.getEntity();

    System.out.println("----------------------------------------");
    System.out.println(response.getStatusLine());
    if (entity != null) {
        System.out.println("Response content length: " + entity.getContentLength());
    }/*ww  w . j ava2s .c  o  m*/
    List<Cookie> cookies = cookieStore.getCookies();
    for (int i = 0; i < cookies.size(); i++) {
        System.out.println("Local cookie: " + cookies.get(i));
    }

    // Consume response content
    if (entity != null) {
        entity.consumeContent();
    }

    System.out.println("----------------------------------------");

    // When HttpClient instance is no longer needed, 
    // shut down the connection manager to ensure
    // immediate deallocation of all system resources
    httpclient.getConnectionManager().shutdown();
}

From source file:com.hilatest.httpclient.apacheexample.ClientCustomContext.java

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

    HttpClient httpclient = new DefaultHttpClient();

    // Create a local instance of cookie store
    CookieStore cookieStore = new BasicCookieStore();

    // Create local HTTP context
    HttpContext localContext = new BasicHttpContext();
    // Bind custom cookie store to the local context
    localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);

    HttpGet httpget = new HttpGet("http://www.google.com/");

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

    // Pass local context as a parameter
    HttpResponse response = httpclient.execute(httpget, localContext);
    HttpEntity entity = response.getEntity();

    System.out.println("----------------------------------------");
    System.out.println(response.getStatusLine());
    if (entity != null) {
        System.out.println("Response content length: " + entity.getContentLength());
    }//w ww  . ja  va 2s .c  o m
    List<Cookie> cookies = cookieStore.getCookies();
    for (int i = 0; i < cookies.size(); i++) {
        System.out.println("Local cookie: " + cookies.get(i));
    }

    // Consume response content
    if (entity != null) {
        entity.consumeContent();
    }

    System.out.println("----------------------------------------");

    // When HttpClient instance is no longer needed, 
    // shut down the connection manager to ensure
    // immediate deallocation of all system resources
    httpclient.getConnectionManager().shutdown();
}

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 {/* w w w .j  ava2s.  c  om*/
        // 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.dlmu.heipacker.crawler.client.ClientCustomContext.java

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

    HttpClient httpclient = new DefaultHttpClient();
    try {/*from   w  w w .  java2s . c o  m*/
        // Create a local instance of cookie store
        CookieStore cookieStore = new BasicCookieStore();

        // Create local HTTP context
        HttpContext localContext = new BasicHttpContext();
        // Bind custom cookie store to the local context
        localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);

        HttpGet httpget = new HttpGet("http://www.google.com/");

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

        // Pass local context as a parameter
        HttpResponse response = httpclient.execute(httpget, localContext);
        HttpEntity entity = response.getEntity();

        System.out.println("----------------------------------------");
        System.out.println(response.getStatusLine());
        if (entity != null) {
            System.out.println("Response content length: " + entity.getContentLength());
        }
        List<Cookie> cookies = cookieStore.getCookies();
        for (int i = 0; i < cookies.size(); i++) {
            System.out.println("Local cookie: " + cookies.get(i));
        }

        // Consume response content
        EntityUtils.consume(entity);

        System.out.println("----------------------------------------");

    } finally {
        // When HttpClient instance is no longer needed,
        // shut down the connection manager to ensure
        // immediate deallocation of all system resources
        httpclient.getConnectionManager().shutdown();
    }
}

From source file:com.mtea.macrotea_httpclient_study.ClientCustomContext.java

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

    HttpClient httpclient = new DefaultHttpClient();
    try {/*from w w  w  .j  av a 2 s  .  c o  m*/
        // CookieStore 
        CookieStore cookieStore = new BasicCookieStore();

        // HttpContext 
        HttpContext localContext = new BasicHttpContext();

        //HttpContextCookieStore
        localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);

        HttpGet httpget = new HttpGet("http://www.google.com/");

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

        // ?
        HttpResponse response = httpclient.execute(httpget, localContext);
        HttpEntity entity = response.getEntity();

        System.out.println("----------------------------------------");
        System.out.println(response.getStatusLine());
        if (entity != null) {
            System.out.println("Response content length: " + entity.getContentLength());
        }

        //???cookie
        List<Cookie> cookies = cookieStore.getCookies();
        for (int i = 0; i < cookies.size(); i++) {
            System.out.println("Local cookie: " + cookies.get(i));
        }

        //
        EntityUtils.consume(entity);

        System.out.println("----------------------------------------");

    } finally {
        //??httpclient???
        httpclient.getConnectionManager().shutdown();
    }
}

From source file:test.AsyncClientCustomContext.java

public final static void main(String[] args) throws Exception {
    CloseableHttpAsyncClient httpclient = HttpAsyncClients.createDefault();
    HttpEntity entity = null;/*from  w  w  w . ja  va2  s  .  co 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();
    }
}

From source file:net.bither.bitherj.api.CookieFactory.java

public synchronized static boolean initCookie() {
    boolean success = true;
    isRunning = true;/*from   www .  ja v  a 2  s. c  o m*/
    CookieStore cookieStore = AbstractApp.bitherjSetting.getCookieStore();
    if (cookieStore.getCookies() == null || cookieStore.getCookies().size() == 0) {
        try {
            GetCookieApi getCookieApi = new GetCookieApi();
            getCookieApi.handleHttpPost();
            // log.debug("getCookieApi");
        } catch (Exception e) {
            success = false;
            e.printStackTrace();
        }
    }
    isRunning = false;
    return success;

}