List icon Contents

From 7.17 to 7.18

Cookies

Deleting cookie

v7.17

When you delete a cookie, the CookieStore.delete(Cookie) method returns a boolean value indicating whether the given cookie was deleted or not.

Java
Kotlin
if (!cookieStore.delete(cookie)) {
    // Error: The cookie hasn't been deleted.
}
if (!cookieStore.delete(cookie)) {
    // Error: The cookie hasn't been deleted.
}

v7.18

In this version we simplified this functionality. Now, the CookieStore.delete(Cookie) method returns void. If the cookie hasn’t been deleted because it’s invalid, or it doesn’t exist, the method does nothing:

Java
Kotlin
cookieStore.delete(cookie);
cookieStore.delete(cookie)

Setting cookie

v7.17

To set a cookie you use the CookieStore.put(String url, Cookie cookie) method. This method requires two parameters: the cookie and URL. If the given URL and the cookie’s domain are different, the cookie will not be set, and the method returns false. So, make sure that the given URL and the cookie domain are the same domains.

Java
Kotlin
Cookie cookie = Cookie.newBuilder()
        .name("cookieName")
        .value("cookieValue")
        .domain(".google.com")
        .secure(true)
        .path("/")
        .build();
if (!cookieStore.put("https://www.google.com", cookie)) {
    // Error: The cookie hasn't been set.
}
Cookie cookie = Cookie.newBuilder()
        .name("cookieName")
        .value("cookieValue")
        .domain(".google.com")
        .secure(true)
        .path("/")
        .build()
if (!cookieStore.put("https://www.google.com", cookie)) {
    // Error: The cookie hasn't been set.
}

v7.18

We figured out that the URL parameter is used by Chromium only to validate the given cookie and check its domain. We decided to simplify this API. Now, you don’t have to pass the URL parameter and make sure that the URL and cookie’s domain are the same. Now, you can set a cookie using the following way:

Java
Kotlin
cookieStore.set(Cookie.newBuilder(".google.com")
        .name("cookieName")
        .value("cookieValue")
        .secure(true)
        .path("/")
        .build());
cookieStore.set(Cookie.newBuilder(".google.com")
        .name("cookieName")
        .value("cookieValue")
        .secure(true)
        .path("/")
        .build())

If the given cookie is invalid and cannot be set, the method throws java.lang.IllegalArgumentException.