Note: Advice in this article will only work for JxBrowser 6. See the corresponding article for JxBrowser 7 here.(注意:本文中的建议仅适用于JxBrowser6,JxBrowser7相应文章请点击这里。)


Each CookieStorage stores cookies on a local file system in Chromium's user data directory. So, each CookieStorage depends on BrowserContext that defines where Chromium's user data folder is located. If you need to use different cookie storages for different Browser instances, then you need to configure Browser instances with different BrowserContext instances and different Chromium's user data directories.(每个CookieStorage都会将Cookie存储在Chromium的用户数据目录中的本地文件系统上。 因此,每个CookieStorage都依赖于定义Chromium的用户数据文件夹所在位置的BrowserContext。 如果需要为不同的浏览器实例使用不同的cookie存储,则需要为浏览器实例配置不同的BrowserContext实例和不同的Chromium用户数据目录)

Browser instances with same BrowserContext instance will share the cookies.(具有相同BrowserContext实例的Browser实例将共享cookie)

The following code demonstrates how to create two Browser instances that don't share cookies: (以下代码演示了如何创建两个不共享Cookie的浏览器实例:)

Browser browserOne = new Browser(new BrowserContext("C:\\MyUserData1"));
Browser browserTwo = new Browser(new BrowserContext("C:\\MyUserData2"));

To access CookieStorage of Browser instance use the browser.getCookieStorage() method. Using CookieStorage you can get, modify, and delete cookies. When you modify cookies you must save changes using the CookieStorage.save() method. For example: (要访问Browser实例的CookieStorage,请使用browser.getCookieStorage()方法。使用CookieStorage,您可以获取,修改和删除Cookie。修改Cookie时,必须使用CookieStorage.save()方法保存更改。例如:)

CookieStorage cookieStorage = browser.getCookieStorage();
cookieStorage.setSessionCookie("http://www.a<span class="fr-marker" data-id="0" data-type="false" style="display: none; line-height: 0;"></span><span class="fr-marker" data-id="0" data-type="true" style="display: none; line-height: 0;"></span>.com", "name1", "value1", ".a.com", "/", false, false);
cookieStorage.save();

JxBrowser supports cookies. Cookies are stored in Chromium user data directory (persistent cookies) and application memory (session cookies). If you delete Chromium user data directory, then all persistent cookies will be removed. Session cookies will be removed automatically when corresponding application terminates. Apart from persistent and session cookies, JxBrowser supports secure and http-only cookies.(JxBrowser支持cookie。 Cookies存储在Chromium用户数据目录(永久cookie)和应用程序内存(会话cookie)中。如果您删除Chromium用户数据目录,则所有永久性Cookie都将被删除。当相应的应用程序终止时,会话cookie将被自动删除。除了持久性和会话cookie外,JxBrowser还支持安全和仅HTTP cookie。)


Getting All Cookies (获取所有Cookie)

List<Cookie> cookies = cookieStorage.getAllCookies();
for (Cookie cookie : cookies) {
    System.out.println("cookie = " + cookie);
}


Getting All Cookies by URL (通过URL获取所有Cookie)

List<Cookie> cookies = cookieStorage.getAllCookies("http://www.google.com");
for (Cookie cookie : cookies) {
    System.out.println("cookie = " + cookie);
}

Creating a Cookie (创建一个cookie)

// Create and add new cookie
final int oneHourInMilliseconds = 36000000;
final int microsecondsOffset = 1000;
// Cookie will be alive during one hour starting from now
long expirationTimeInMicroseconds = (System.currentTimeMillis() + 
        oneHourInMilliseconds) * microsecondsOffset;
cookieStorage.setCookie("http://www.google.com", "mycookie", 
        "myvalue", ".google.com", "/", expirationTimeInMicroseconds, 
        false, false);
cookieStorage.save();

Creating a Session Cookie (创建会话Cookie)

// Create and add new session cookie
cookieStorage.setSessionCookie("http://www.google.com", "mycookie", 
        "myvalue", ".google.com", "/", false, false);
cookieStorage.save();

Deleting All Cookies (删除所有Cookie)

int numberOfDeletedCookies = cookieStorage.deleteAll();
cookieStorage.save();

Deleting a Cookie (删除Cookie)


List<Cookie> cookies = cookieStorage.getAllCookies();
for (Cookie cookie : cookies) {
    boolean success = cookieStorage.delete(cookie);    
}
cookieStorage.save();