Example usage for java.net CookieStore get

List of usage examples for java.net CookieStore get

Introduction

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

Prototype

public List<HttpCookie> get(URI uri);

Source Link

Document

Retrieve cookies associated with given URI, or whose domain matches the given URI.

Usage

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.get(new URI("http://google.com"));
    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();//from   ww  w  . j  av a2s.  co  m
    }
}

From source file:org.jboss.aerogear.android.authentication.digest.DigestAuthenticationModuleRunner.java

@Override
public void onLogout() {
    HttpProvider provider = httpProviderFactory.get(logoutURL, timeout);

    clear();/*from   ww  w. j  a va2s.co  m*/

    CookieStore store = ((CookieManager) CookieManager.getDefault()).getCookieStore();
    List<HttpCookie> cookies = store.get(getBaseURI());

    for (HttpCookie cookie : cookies) {
        store.remove(getBaseURI(), cookie);
    }

    provider.post("");
}

From source file:de.fh_zwickau.informatik.sensor.ZWayApiHttp.java

@Override
public synchronized String getLogin() {
    try {/*  w  w w .  ja v  a 2 s .c  o  m*/
        startHttpClient();

        if (mUseRemoteService) {
            mZWaySessionId = null;
            mZWayRemoteSessionId = null;

            // Build request body
            String body = "act=login&login=" + mRemoteId + "/" + mUsername + "&pass=" + mPassword;

            logger.info("Remote login body: {}", body);
            logger.info("Remote path: {}", getTopLevelUrl() + "/" + REMOTE_PATH_LOGIN);

            Request request = mHttpClient.newRequest(getTopLevelUrl() + "/" + REMOTE_PATH_LOGIN)
                    .method(HttpMethod.POST)
                    .header(HttpHeader.CONTENT_TYPE, "application/x-www-form-urlencoded")
                    .content(new StringContentProvider(body), "application/x-www-form-urlencoded");

            ContentResponse response = request.send();

            // Check HTTP status code
            int statusCode = response.getStatus();

            if (statusCode != HttpStatus.OK_200) {
                String reason = response.getReason();
                mCaller.httpStatusError(statusCode, reason, true);
                logger.debug("Communication with Z-Way server failed: {} {}", statusCode, reason);
            }

            CookieStore cookieStore = mHttpClient.getCookieStore();
            List<HttpCookie> cookies = cookieStore.get(URI.create("https://find.z-wave.me/"));
            for (HttpCookie cookie : cookies) {
                if (cookie.getName().equals("ZWAYSession")) {
                    mZWaySessionId = cookie.getValue();
                } else if (cookie.getName().equals("ZBW_SESSID")) {
                    mZWayRemoteSessionId = cookie.getValue();
                }
                logger.info("HTTP cookie: {} - {}", cookie.getName(), cookie.getValue());
            }

            if (mZWayRemoteSessionId != null && mZWaySessionId != null) {
                mCaller.getLoginResponse(mZWaySessionId);
                return mZWaySessionId;
            } else {
                logger.warn("Response doesn't contain required cookies");
                mCaller.responseFormatError("Response doesn't contain required cookies", true);
            }
        } else {
            // Build request body
            LoginForm loginForm = new LoginForm(true, mUsername, mPassword, false, 1);

            Request request = mHttpClient.newRequest(getZAutomationTopLevelUrl() + "/" + PATH_LOGIN)
                    .method(HttpMethod.POST).header(HttpHeader.ACCEPT, "application/json")
                    .header(HttpHeader.CONTENT_TYPE, "application/json")
                    .content(new StringContentProvider(new Gson().toJson(loginForm)), "application/json");

            ContentResponse response = request.send();

            // Check HTTP status code
            int statusCode = response.getStatus();
            if (statusCode != HttpStatus.OK_200) {
                String reason = response.getReason();
                mCaller.httpStatusError(statusCode, reason, true);
                logger.debug("Communication with Z-Way server failed: {} {}", statusCode, reason);
            }

            String responseBody = response.getContentAsString();
            try {
                Gson gson = new Gson();
                // Response -> String -> Json -> extract data field
                JsonObject responseDataAsJson = gson.fromJson(responseBody, JsonObject.class).get("data")
                        .getAsJsonObject(); // extract data field

                mZWaySessionId = responseDataAsJson.get("sid").getAsString(); // extract SID field
                mCaller.getLoginResponse(mZWaySessionId);
                return mZWaySessionId;
            } catch (JsonParseException e) {
                logger.warn("Unexpected response format: {}", e.getMessage());
                mCaller.responseFormatError("Unexpected response format: " + e.getMessage(), true);
            }
        }
    } catch (Exception e) {
        logger.warn("Request getLogin() failed: {}", e.getMessage());
        mCaller.apiError(e.getMessage(), true);
    } finally {
        stopHttpClient();
    }

    mZWaySessionId = null;
    mZWayRemoteSessionId = null;
    mCaller.authenticationError();
    return null;
}