Example usage for java.net CookieStore getCookies

List of usage examples for java.net CookieStore getCookies

Introduction

In this page you can find the example usage for java.net CookieStore getCookies.

Prototype

public List<HttpCookie> getCookies();

Source Link

Document

Get all not-expired cookies in cookie store.

Usage

From source file:Main.java

public static void main(String args[]) throws Exception {
    String urlString = "http://google.com";
    CookieManager manager = new CookieManager();
    CookieHandler.setDefault(manager);
    URL url = new URL(urlString);
    URLConnection connection = url.openConnection();
    Object obj = connection.getContent();
    url = new URL(urlString);
    connection = url.openConnection();/*  www . ja v  a  2 s .c om*/
    obj = connection.getContent();
    CookieStore cookieJar = manager.getCookieStore();
    List<HttpCookie> cookies = cookieJar.getCookies();
    for (HttpCookie cookie : cookies) {
        System.out.println(cookie);
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    CookieManager cm = new CookieManager();
    cm.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
    CookieHandler.setDefault(cm);

    new URL("http://google.com").openConnection().getContent();
    CookieStore cookieStore = cm.getCookieStore();

    List<HttpCookie> cookies = cookieStore.getCookies();
    for (HttpCookie cookie : cookies) {
        System.out.println("Name = " + cookie.getName());
        System.out.println("Value = " + cookie.getValue());
        System.out.println("Lifetime (seconds) = " + cookie.getMaxAge());
        System.out.println("Path = " + cookie.getPath());
        System.out.println();// www  . j av  a  2s.c o  m
    }
}

From source file:FetchCookie.java

public static void main(String args[]) throws Exception {
    String urlString = "http://java.sun.com";
    CookieManager manager = new CookieManager();
    CookieHandler.setDefault(manager);
    URL url = new URL(urlString);
    URLConnection connection = url.openConnection();
    Object obj = connection.getContent();
    url = new URL(urlString);
    connection = url.openConnection();//from  ww  w.  ja  va  2s. c  o  m
    obj = connection.getContent();
    CookieStore cookieJar = manager.getCookieStore();
    List<HttpCookie> cookies = cookieJar.getCookies();
    for (HttpCookie cookie : cookies) {
        System.out.println(cookie);
    }
}

From source file:org.apache.hadoop.hive.druid.security.DruidKerberosUtil.java

static HttpCookie getAuthCookie(CookieStore cookieStore, URI uri) {
    if (cookieStore == null) {
        return null;
    }//w w  w.  j  av a 2  s .c  om
    boolean isSSL = uri.getScheme().equals("https");
    List<HttpCookie> cookies = cookieStore.getCookies();

    for (HttpCookie c : cookies) {
        // If this is a secured cookie and the current connection is non-secured,
        // then, skip this cookie. We need to skip this cookie because, the cookie
        // replay will not be transmitted to the server.
        if (c.getSecure() && !isSSL) {
            continue;
        }
        if (c.getName().equals(AuthenticatedURL.AUTH_COOKIE)) {
            return c;
        }
    }
    return null;
}

From source file:io.druid.security.kerberos.DruidKerberosUtil.java

public static HttpCookie getAuthCookie(CookieStore cookieStore, URI uri) {
    if (cookieStore == null) {
        return null;
    }//from  ww w. java2s  . c  o  m
    boolean isSSL = uri.getScheme().equals("https");
    List<HttpCookie> cookies = cookieStore.getCookies();

    for (HttpCookie c : cookies) {
        // If this is a secured cookie and the current connection is non-secured,
        // then, skip this cookie. We need to skip this cookie because, the cookie
        // replay will not be transmitted to the server.
        if (c.getSecure() && !isSSL) {
            continue;
        }
        if (c.getName().equals(AuthenticatedURL.AUTH_COOKIE)) {
            return c;
        }
    }
    return null;
}

From source file:org.apache.druid.security.kerberos.DruidKerberosUtil.java

public static HttpCookie getAuthCookie(CookieStore cookieStore, URI uri) {
    if (cookieStore == null) {
        return null;
    }// www  .j  a  v  a2 s  . c o m
    boolean isSSL = "https".equals(uri.getScheme());
    List<HttpCookie> cookies = cookieStore.getCookies();

    for (HttpCookie c : cookies) {
        // If this is a secured cookie and the current connection is non-secured,
        // then, skip this cookie. We need to skip this cookie because, the cookie
        // replay will not be transmitted to the server.
        if (c.getSecure() && !isSSL) {
            continue;
        }
        if (c.getName().equals(AuthenticatedURL.AUTH_COOKIE)) {
            return c;
        }
    }
    return null;
}

From source file:cn.ttyhuo.common.MyApplication.java

public static void setUpPersistentCookieStore() {
    if (context == null)
        return;// w w w  .  jav a  2s .co  m
    cookieStore = new PersistentCookieStore(context);

    CookieStore httpCookieStore = HttpRequestUtil.cookieManager.getCookieStore();
    for (HttpCookie h : httpCookieStore.getCookies()) {
        BasicClientCookie newCookie = new BasicClientCookie(h.getName(), h.getValue());
        newCookie.setVersion(h.getVersion());
        newCookie.setDomain(h.getDomain());
        newCookie.setPath(h.getPath());
        newCookie.setSecure(h.getSecure());
        newCookie.setComment(h.getComment());
        cookieStore.addCookie(newCookie);
    }
}

From source file:net.qiujuer.common.okhttp.Http.java

public static String getCookie() {
    CookieHandler handler = getClient().getCookieHandler();
    if (handler != null && handler instanceof CookieManager) {
        CookieManager manager = (CookieManager) handler;
        CookieStore store = manager.getCookieStore();
        if (store != null) {
            Util.log(store.toString());
            try {
                List<HttpCookie> cookies = store.getCookies();
                if (cookies.size() > 0) {
                    String cookieStr = "";
                    for (HttpCookie cookie : cookies) {
                        cookieStr += cookie.toString();
                    }// ww  w .  j  a v a 2s . co m
                    return cookieStr;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    return "";
}

From source file:horriblev3.Cloudflare.java

private List<HttpCookie> retrieveCookies(CookieManager manager) {
    java.net.CookieStore cookieJar = manager.getCookieStore();
    return cookieJar.getCookies();
}

From source file:CookieAccessor.java

/**
 * Get cookies for a url from cookie store
 *///ww w  . j  a  va 2s.  c om
public void getCookieUsingCookieHandler() {
    try {
        // instantiate CookieManager; make sure to set CookiePolicy
        CookieManager manager = new CookieManager();
        manager.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
        CookieHandler.setDefault(manager);

        // get content from URLConnection; cookies are set by web site
        URL url = new URL("http://host.example.com");
        URLConnection connection = url.openConnection();
        connection.getContent();

        // get cookies from underlying CookieStore
        CookieStore cookieJar = manager.getCookieStore();
        List<HttpCookie> cookies = cookieJar.getCookies();
        for (HttpCookie cookie : cookies) {
            System.out.println("CookieHandler retrieved cookie: " + cookie);
        }
    } catch (Exception e) {
        System.out.println("Unable to get cookie using CookieHandler");
        e.printStackTrace();
    }
}