A headless browser for Java programs that provides web automation, form handling, JavaScript execution, and DOM manipulation capabilities.
—
Cookie handling and session management providing automatic cookie processing, manual cookie manipulation, and session persistence for maintaining authentication and state across page navigations.
Configure cookie behavior and access the cookie management system.
/**
* Get the cookie manager for this client
* @return CookieManager instance for this WebClient
*/
public CookieManager getCookieManager();
/**
* Set a custom cookie manager
* @param manager custom CookieManager implementation
*/
public void setCookieManager(CookieManager manager);Usage Examples:
WebClient webClient = new WebClient();
CookieManager cookieManager = webClient.getCookieManager();
// Enable/disable cookie processing
cookieManager.setCookiesEnabled(true);Add, remove, and query cookies programmatically.
public class CookieManager {
/**
* Enable or disable automatic cookie handling
* @param enabled true to process cookies automatically
*/
public void setCookiesEnabled(boolean enabled);
/**
* Check if cookie processing is enabled
* @return true if cookies are being processed
*/
public boolean isCookiesEnabled();
/**
* Get all cookies currently stored
* @return Set of Cookie objects
*/
public Set<Cookie> getCookies();
/**
* Add a cookie to the cookie store
* @param cookie Cookie object to add
*/
public void addCookie(Cookie cookie);
/**
* Remove a specific cookie from the store
* @param cookie Cookie object to remove
*/
public void removeCookie(Cookie cookie);
/**
* Clear all expired cookies from the store
* @param date current date for expiration check
*/
public void clearExpired(Date date);
/**
* Remove all cookies from the store
*/
public void clearCookies();
/**
* Get cookies for a specific domain
* @param domain the domain to filter by
* @return Set of cookies for the specified domain
*/
public Set<Cookie> getCookies(String domain);
}Usage Examples:
CookieManager cookieManager = webClient.getCookieManager();
// Add a session cookie
Cookie sessionCookie = new Cookie("example.com", "sessionId", "abc123");
cookieManager.addCookie(sessionCookie);
// Add an authentication cookie with path and expiration
Date expiry = new Date(System.currentTimeMillis() + 86400000); // 24 hours
Cookie authCookie = new Cookie("example.com", "authToken", "xyz789");
authCookie.setPath("/api");
authCookie.setExpires(expiry);
authCookie.setSecure(true);
cookieManager.addCookie(authCookie);
// Get all cookies
Set<Cookie> allCookies = cookieManager.getCookies();
for (Cookie cookie : allCookies) {
System.out.println(cookie.getName() + " = " + cookie.getValue());
}
// Get cookies for specific domain
Set<Cookie> domainCookies = cookieManager.getCookies("example.com");
// Clear expired cookies
cookieManager.clearExpired(new Date());
// Remove all cookies
cookieManager.clearCookies();Individual cookie properties and manipulation.
public class Cookie {
/**
* Create a basic cookie
* @param domain the domain this cookie belongs to
* @param name the cookie name
* @param value the cookie value
*/
public Cookie(String domain, String name, String value);
/**
* Create a cookie with path specification
* @param domain the domain this cookie belongs to
* @param name the cookie name
* @param value the cookie value
* @param path the cookie path
* @param expires expiration date, null for session cookie
* @param secure whether cookie requires HTTPS
*/
public Cookie(String domain, String name, String value, String path, Date expires, boolean secure);
/**
* Get the cookie name
* @return the cookie name
*/
public String getName();
/**
* Get the cookie value
* @return the cookie value
*/
public String getValue();
/**
* Set the cookie value
* @param value the new cookie value
*/
public void setValue(String value);
/**
* Get the cookie domain
* @return the domain this cookie applies to
*/
public String getDomain();
/**
* Set the cookie domain
* @param domain the domain this cookie applies to
*/
public void setDomain(String domain);
/**
* Get the cookie path
* @return the path this cookie applies to
*/
public String getPath();
/**
* Set the cookie path
* @param path the path this cookie applies to
*/
public void setPath(String path);
/**
* Get the cookie expiration date
* @return expiration date, or null for session cookies
*/
public Date getExpires();
/**
* Set the cookie expiration date
* @param expires expiration date, null for session cookie
*/
public void setExpires(Date expires);
/**
* Check if cookie requires HTTPS
* @return true if cookie is secure
*/
public boolean isSecure();
/**
* Set whether cookie requires HTTPS
* @param secure true to require HTTPS
*/
public void setSecure(boolean secure);
/**
* Check if cookie is HTTP-only (not accessible from JavaScript)
* @return true if cookie is HTTP-only
*/
public boolean isHttpOnly();
/**
* Set whether cookie is HTTP-only
* @param httpOnly true to make cookie HTTP-only
*/
public void setHttpOnly(boolean httpOnly);
/**
* Check if cookie has expired
* @return true if cookie has expired
*/
public boolean isExpired();
/**
* Check if cookie has expired relative to a specific date
* @param date the reference date for expiration check
* @return true if cookie has expired
*/
public boolean isExpired(Date date);
}Usage Examples:
// Create basic session cookie
Cookie sessionCookie = new Cookie("example.com", "sessionId", "abc123");
// Create secure authentication cookie
Cookie authCookie = new Cookie("api.example.com", "token", "secure_token_value");
authCookie.setPath("/api");
authCookie.setSecure(true);
authCookie.setHttpOnly(true);
// Set expiration for 1 hour from now
Date expiry = new Date(System.currentTimeMillis() + 3600000);
authCookie.setExpires(expiry);
// Cookie properties
String name = authCookie.getName(); // "token"
String value = authCookie.getValue(); // "secure_token_value"
String domain = authCookie.getDomain(); // "api.example.com"
String path = authCookie.getPath(); // "/api"
boolean secure = authCookie.isSecure(); // true
boolean httpOnly = authCookie.isHttpOnly(); // true
boolean expired = authCookie.isExpired(); // false (assuming current time)
// Modify cookie
authCookie.setValue("new_token_value");
authCookie.setPath("/");Common patterns for managing user sessions and authentication cookies.
Usage Examples:
WebClient webClient = new WebClient();
CookieManager cookieManager = webClient.getCookieManager();
// Automated login with session management
HtmlPage loginPage = webClient.getPage("https://example.com/login");
HtmlForm loginForm = loginPage.getFormByName("loginForm");
loginForm.getInputByName("username").setValue("myuser");
loginForm.getInputByName("password").setValue("mypass");
// Submit login form - cookies are automatically captured
HtmlPage welcomePage = loginForm.submit();
// Check that session cookies were set
Set<Cookie> cookies = cookieManager.getCookies("example.com");
boolean hasSessionCookie = cookies.stream()
.anyMatch(cookie -> "sessionId".equals(cookie.getName()));
if (hasSessionCookie) {
System.out.println("Successfully logged in with session");
// Navigate to protected pages - session cookies are automatically sent
HtmlPage profilePage = webClient.getPage("https://example.com/profile");
HtmlPage settingsPage = webClient.getPage("https://example.com/settings");
}
// Manually add authentication cookie for API access
Cookie apiToken = new Cookie("api.example.com", "X-Auth-Token", "bearer_token_123");
apiToken.setPath("/api/v1");
apiToken.setSecure(true);
cookieManager.addCookie(apiToken);
// API requests will automatically include the cookie
WebRequest apiRequest = new WebRequest(new URL("https://api.example.com/api/v1/data"));
Page apiResponse = webClient.getPage(apiRequest);Handle cookie storage and restoration across WebClient sessions.
Usage Examples:
// Save cookies to restore in future sessions
WebClient webClient = new WebClient();
// ... perform operations that set cookies ...
Set<Cookie> savedCookies = webClient.getCookieManager().getCookies();
// Later: restore cookies in new WebClient session
WebClient newWebClient = new WebClient();
CookieManager newCookieManager = newWebClient.getCookieManager();
for (Cookie cookie : savedCookies) {
if (!cookie.isExpired()) {
newCookieManager.addCookie(cookie);
}
}
// The new client now has access to the previous session's cookiesInstall with Tessl CLI
npx tessl i tessl/maven-org-htmlunit--htmlunit