- Spell Checker
This document describes how to work with cookies.
JxBrowser delegates the work with cookies to the Chromium engine. Chromium decides how to download cookies from a web server, extract them from the HTTP headers and store them in a local file system (persistent cookies) or in the memory (session cookies).
CookieStore class allows you to get, modify, and remove cookies. The
Cookie class provides information on a particular cookie.
JxBrowser supports cookies that are sent using the following protocols:
- WS (WebSocket)
- WSS (Secured WebSocket)
If a cookie is sent using a protocol that is not on the list, e.g.
ftp://, it will not be stored in the cookie storage.
Working with Cookies
JxBrowser supports the following kinds of cookies:
- Persistent cookies — these are stored in the Chromium user data directory. If you delete the Chromium user data directory, all the persistent cookies will be removed.
- Session cookies — these are stored in the application memory. These cookies will be removed automatically when the application is terminated.
- Secure cookies — these can only be transmitted over an encrypted connection, i.e. HTTPS. This makes the cookie less likely to be exposed to cookie theft via eavesdropping.
When you modify cookies please use the
CookieStore.persist() method to save the changes.
To get all cookies, please use the
cookieStore.cookies().forEach(cookie -> System.out.println("cookie = " + cookie));
To get all cookies by a URL, please use the
getCookies() method that accepts a string:
cookieStore.cookies("https://www.google.com").forEach(cookie -> System.out.println("cookie = " + cookie));
To create a persistent cookie use the following code:
boolean success = cookieStore.put("http://www.google.com", Cookie.newBuilder() .creationTime(creationTime) .expirationTime(expirationTime) .name("name") .value("value") .domain(".google.com") .path("/") .build()); cookieStore.persist();
The code above creates a persistent cookie for the
http://www.google.com URL. The
success variable will be
true if the cookie is created and added to the cookie storage successfully.
To create a session cookie use the following code:
boolean success = cookieStore.put("http://www.google.com", Cookie.newBuilder() .name("name") .value("value") .domain(".google.com") .path("/") .build()); cookieStore.persist();
To delete all cookies use the
int numberOfDeletedCookies = cookieStore.deleteAll(); cookieStore.persist();
To delete one cookie, please use the
delete(Cookie). The following code deletes all cookies one by one obtaining the result of the operation:
You can control all incoming and outgoing cookies using the
CanGetCookiesCallback callbacks of the
To suppress the incoming cookies use the following code:
network.set(CanSetCookieCallback.class, params -> Response.cannot());
To suppress the outgoing cookies use the following code:
network.set(CanGetCookiesCallback.class, params -> Response.cannot());
JxBrowser supports the cookie encryption by default. It uses the Chromium cookies encryption routines, so the cookies are stored exactly as in Chromium.
On Linux JxBrowser uses GNOME Keyring or KWallet to encrypt cookies. The library automatically chooses which store to use. You can manually specify which store to use via an appropriate option when constructing the
Engine. For example:
Engine engine = Engine.newInstance(EngineOptions.newBuilder(...) .passwordStore(PasswordStore.GNOME_KEYRING) .build());
On Windows JxBrowser uses only DPAPI to encrypt cookies. There are no alternatives at the moment.
On macOS JxBrowser uses the private key stored with the Keychain Application to encrypt cookies with AES encryption.