3.5. Cookie persistence
HttpClient can work with any physical representation of a persistent cookie store that implements
the CookieStore interface. The default CookieStore implementation called BasicCookieStore is aHTTP state management
27
simple implementation backed by a java.util.ArrayList. Cookies stored in an BasicClientCookie
object are lost when the container object get garbage collected. Users can provide more complex
implementations if necessary.
// Create a local instance of cookie store
CookieStore cookieStore = new BasicCookieStore();
// Populate cookies if needed
BasicClientCookie cookie = new BasicClientCookie("name", "value");
cookie.setVersion(0);
cookie.setDomain(".mycompany.com");
cookie.setPath("/");
cookieStore.addCookie(cookie);
// Set the store
CloseableHttpClient httpclient = HttpClients.custom()
.setDefaultCookieStore(cookieStore)
.build();
3.5. Cookie persistence
HttpClient can work with any physical representation of a persistent cookie store that implements
the CookieStore interface. The default CookieStore implementation called BasicCookieStore is aHTTP state management
27
simple implementation backed by a java.util.ArrayList. Cookies stored in an BasicClientCookie
object are lost when the container object get garbage collected. Users can provide more complex
implementations if necessary.
// Create a local instance of cookie store
CookieStore cookieStore = new BasicCookieStore();
// Populate cookies if needed
BasicClientCookie cookie = new BasicClientCookie("name", "value");
cookie.setVersion(0);
cookie.setDomain(".mycompany.com");
cookie.setPath("/");
cookieStore.addCookie(cookie);
// Set the store
CloseableHttpClient httpclient = HttpClients.custom()
.setDefaultCookieStore(cookieStore)
.build();
3.5. Cookie persistence
HttpClient can work with any physical representation of a persistent cookie store that implements
the CookieStore interface. The default CookieStore implementation called BasicCookieStore is aHTTP state management
27
simple implementation backed by a java.util.ArrayList. Cookies stored in an BasicClientCookie
object are lost when the container object get garbage collected. Users can provide more complex
implementations if necessary.
// Create a local instance of cookie store
CookieStore cookieStore = new BasicCookieStore();
// Populate cookies if needed
BasicClientCookie cookie = new BasicClientCookie("name", "value");
cookie.setVersion(0);
cookie.setDomain(".mycompany.com");
cookie.setPath("/");
cookieStore.addCookie(cookie);
// Set the store
CloseableHttpClient httpclient = HttpClients.custom()
.setDefaultCookieStore(cookieStore)
.build();