getCookies

Cookie[] session.getCookies ( )

Description

Get the current cookies.

Parameters

This method does not receive any parameters.

Return Values

Returns an array of the cookies in the session.

Change Log

Version Description
5.0 Available for all editions.

Examples

Add Cookie If Missing

// Get cookies
cookies = session.getCookies();

// Cookie Information
cookieDomain = "mydomain.com";
cookieName = "cookie_test";
cookieValue = "please_accept_for_session";

// Exists Flag
cookieExists = false;

// Loop through cookies
for (i = 0; i < cookies.length; i++) {
    cookie = cookies[i];

    // Check if this is the cookie
    if (cookie.getName().equals(cookieName) && cookie.getValue().equals(cookieValue)&&cookie.getDomain().equals(cookieDomain)) {
        //if the cookie matches then it exists
        cookieExists = true;
        // Log search status
        session.log( "+++Cookie Exists" );
        // Stop searching
        break;
    }
}

// Add cookie, if it doesn't exist
if ( !cookieExists ) {
    session.log( "+++Cookie Does NOT Exists: Setting Cookie" );
    session.setCookie( cookieDomain, cookieName, cookieValue);
}

Write Cookies to Log

// Get cookies
cookies = session.getCookies();

// Loop through Cookies
for (i = 0; i < cookies.length; i++) {
    cookie = cookies[i];

    // Write Cookie information to the Log
    session.log( "COOKIE #" + i );
    session.log( "Name: " + cookie.getName() );
    session.log( "Value: " + cookie.getValue() );
    session.log( "Path: " + cookie.getPath() );
    session.log( "Domain: " + cookie.getDomain() );
    // Only log expiration if it is set
    if (cookie.getExpiryDate() != null) {
        session.log( "Expiration: " + cookie.getExpiryDate().toString() );
    }
}

See Also

  • clearCookies() [session] - Clears all the cookies from this scraping session
  • setCookie() [session] - Sets the value of a cookie