Example usage for java.net CookieManager getCookieStore

List of usage examples for java.net CookieManager getCookieStore

Introduction

In this page you can find the example usage for java.net CookieManager getCookieStore.

Prototype

public CookieStore getCookieStore() 

Source Link

Document

To retrieve current 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();/*from w  w w . j  av a 2 s  .com*/
    obj = connection.getContent();
    CookieStore cookieJar = manager.getCookieStore();
    List<HttpCookie> cookies = cookieJar.getCookies();
    for (HttpCookie cookie : cookies) {
        System.out.println(cookie);
    }
}

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 w  w  w  .  ja  va2 s .  co  m*/
    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

/**
 * Clears the cookies in the given cookie handler. Cookies can only be cleared if the
 * cookieHandler is a CookieManager with a non-null CookieStore.
 *
 * @param cookieHandler the cookie handler where cookies should be cleared
 * @return true if cookies were cleared; false otherwise
 *//*  w  w  w  . j a  v a2 s.c  o m*/
public static boolean clearCookies(CookieHandler cookieHandler) {
    if (cookieHandler instanceof CookieManager) {
        CookieManager cookieManager = (CookieManager) cookieHandler;
        CookieStore cookieStore = cookieManager.getCookieStore();
        if (cookieStore != null) {
            cookieStore.removeAll();
            return true;
        }
    }
    return false;
}

From source file:keywhiz.cli.ClientUtils.java

/**
 * Serialize the cookies to JSON from the given CookieManager to a file at the specified path.
 * Output file will have 660 permissions (owner-read, owner-write).
 *
 * @param cookieManager CookieManager that contains cookies to be serialized.
 * @param path Location to serialize cookies to file.
 *///w  ww.  j  a v  a2 s  .c o m
public static void saveCookies(CookieManager cookieManager, Path path) {
    List<HttpCookie> cookies = cookieManager.getCookieStore().getCookies();
    try (BufferedWriter writer = Files.newBufferedWriter(path, CREATE)) {
        Files.setPosixFilePermissions(path, ImmutableSet.of(OWNER_READ, OWNER_WRITE));
        writer.write(Jackson.newObjectMapper().writeValueAsString(
                cookies.stream().map(c -> JsonCookie.fromHttpCookie(c)).collect(Collectors.toList())));
    } catch (IOException e) {
        throw Throwables.propagate(e);
    }
}

From source file:keywhiz.cli.ClientUtils.java

/**
 * Creates a {@link OkHttpClient} to start a TLS connection.
 *
 * @param cookies list of cookies to include in the client.
 * @return new http client./*from  w  w w .  j a  v  a2  s.  c o  m*/
 */
public static OkHttpClient sslOkHttpClient(List<HttpCookie> cookies) {
    checkNotNull(cookies);

    SSLContext sslContext;
    try {
        sslContext = SSLContext.getInstance("TLSv1.2");

        TrustManagerFactory trustManagerFactory = TrustManagerFactory
                .getInstance(TrustManagerFactory.getDefaultAlgorithm());
        trustManagerFactory.init((KeyStore) null);

        sslContext.init(new KeyManager[0], trustManagerFactory.getTrustManagers(), new SecureRandom());
    } catch (NoSuchAlgorithmException | KeyManagementException | KeyStoreException e) {
        throw Throwables.propagate(e);
    }

    SSLSocketFactory socketFactory = sslContext.getSocketFactory();

    OkHttpClient client = new OkHttpClient().setSslSocketFactory(socketFactory)
            .setConnectionSpecs(Arrays.asList(ConnectionSpec.MODERN_TLS)).setFollowSslRedirects(false);

    client.setRetryOnConnectionFailure(false);
    client.networkInterceptors().add(new XsrfTokenInterceptor("XSRF-TOKEN", "X-XSRF-TOKEN"));
    CookieManager cookieManager = new CookieManager();
    cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
    cookies.forEach(c -> cookieManager.getCookieStore().add(null, c));
    client.setCookieHandler(cookieManager);
    return client;
}

From source file:com.hybris.mobile.Hybris.java

public static void clearCookies() {
    CookieManager cookieManager = (CookieManager) CookieHandler.getDefault();
    cookieManager.getCookieStore().removeAll();
}

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

public static void removeCookie() {
    CookieHandler handler = getClient().getCookieHandler();
    if (handler != null && handler instanceof CookieManager) {
        CookieManager manager = (CookieManager) handler;
        CookieStore store = manager.getCookieStore();
        if (store != null)
            store.removeAll();/*from w w  w.ja va 2s .  c  o  m*/
    }
}

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();
                    }// www.ja v  a  2s  .com
                    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:org.nextlets.erc.defaults.http.ERCHttpInvokerImpl.java

private void toCookieManager(URI uri, CookieStore cs, CookieManager cm) {
    cm.getCookieStore().removeAll();
    for (Cookie c : cs.getCookies()) {
        cm.getCookieStore().add(uri, toHttpCookie(c));
    }/*from  w ww. jav  a 2 s.  c om*/
}