下面列出了怎么用java.net.CookieStore的API类实例代码及写法,或者点击链接到github查看源代码。
public void testCookieStoreNullUris() {
CookieStore cookieStore = new CookieManager(createCookieStore(), null).getCookieStore();
HttpCookie cookieA = createCookie("a", "android", ".android.com", "/source");
HttpCookie cookieB = createCookie("b", "banana", "code.google.com", "/p/android");
cookieStore.add(null, cookieA);
assertEquals(Arrays.asList(cookieA), cookieStore.getCookies());
cookieStore.add(null, cookieB);
assertEquals(Arrays.asList(cookieA, cookieB), cookieStore.getCookies());
try {
cookieStore.get(null);
fail();
} catch (NullPointerException expected) {
}
assertEquals(Collections.<URI>emptyList(), cookieStore.getURIs());
assertTrue(cookieStore.remove(null, cookieA));
assertEquals(Arrays.asList(cookieB), cookieStore.getCookies());
assertTrue(cookieStore.removeAll());
assertEquals(Collections.<URI>emptyList(), cookieStore.getURIs());
assertEquals(Collections.<HttpCookie>emptyList(), cookieStore.getCookies());
}
private void resetCookie() {
settings.edit().remove(prefKey).apply();
authCookie = "";
try {
/*
This step is *required* for PNA, and nicety for other services. PNA won't let you
log in if you're still holding on to a valid authcookie, so we clear them out.
*/
URI loginURI = URI.create(url.getHost());
CookieStore cookies = ((CookieManager) CookieHandler.getDefault()).getCookieStore();
for (HttpCookie cookie : cookies.get(loginURI)) {
cookies.remove(loginURI, cookie);
}
} catch (IllegalArgumentException e) {
e.printStackTrace();
}
cookieHasBeenSet = false;
}
protected void setAuthCookieVal(String authCookie, String domain) {
this.authCookie = authCookie;
settings.edit().putString(prefKey, authCookie).apply();
/*
this is technically unnecessary if OkHttp handled the authentication, because it will
have already set the cookies in the CookieHandler. It doesn't seem to cause any issues
just to re-add the cookies, though
*/
HttpCookie httpCookie = new HttpCookie(authCookieKey, authCookie);
httpCookie.setDomain(domain);
try {
CookieStore cookies = ((CookieManager) CookieHandler.getDefault()).getCookieStore();
cookies.add(URI.create(domain), httpCookie);
} catch (IllegalArgumentException e) {
e.printStackTrace();
}
cookieHasBeenSet = true;
android.webkit.CookieManager.getInstance().setCookie(domain, httpCookie.getName() + "=" + httpCookie.getValue());
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP) {
android.webkit.CookieManager.getInstance().flush();
} else {
CookieSyncManager.getInstance().sync();
}
}
public static void main(String[] args) throws IOException, InterruptedException {
/*
HttpClient client = HttpClient.newBuilder()
.cookieHandler(new CookieManager(null, CookiePolicy.ACCEPT_NONE))
.build();
System.out.println(client.cookieHandler().orElseThrow());
*/
CookieManager cm = new CookieManager();
cm.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
HttpClient client = HttpClient.newBuilder()
.cookieHandler(cm)
.build();
HttpRequest request = HttpRequest.newBuilder()
.header("Authorization", "Bearer Q3ku4mp_hCQGvAFeYKa0ktFCDKS3VpSA1nwf")
.uri(URI.create("https://gorest.co.in/public-api/users/1"))
.build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println("Status code: " + response.statusCode());
System.out.println("\n Body: " + response.body());
System.out.println("\nset-cookie header: " + response.headers().firstValue("set-cookie"));
CookieStore cookieStore = cm.getCookieStore();
System.out.println("\nCookies: " + cookieStore.getCookies());
}
static void checkCookieNullUri() throws Exception {
//get a cookie store implementation and add a cookie to the store with null URI
CookieStore cookieStore = (new CookieManager()).getCookieStore();
//Check if removeAll() retrurns false on an empty CookieStore
if (cookieStore.removeAll()) {
fail = true;
}
checkFail("removeAll on empty store should return false");
HttpCookie cookie = new HttpCookie("MY_COOKIE", "MY_COOKIE_VALUE");
cookie.setDomain("foo.com");
cookieStore.add(null, cookie);
//Retrieve added cookie
URI uri = new URI("http://foo.com");
List<HttpCookie> addedCookieList = cookieStore.get(uri);
//Verify CookieStore behaves well
if (addedCookieList.size() != 1) {
fail = true;
}
checkFail("Abnormal size of cookie jar");
for (HttpCookie chip : addedCookieList) {
if (!chip.equals(cookie)) {
fail = true;
}
}
checkFail("Cookie not retrieved from Cookie Jar");
boolean ret = cookieStore.remove(null,cookie);
if (!ret) {
fail = true;
}
checkFail("Abnormal removal behaviour from Cookie Jar");
}
static void checkCookieNullUri() throws Exception {
//get a cookie store implementation and add a cookie to the store with null URI
CookieStore cookieStore = (new CookieManager()).getCookieStore();
//Check if removeAll() retrurns false on an empty CookieStore
if (cookieStore.removeAll()) {
fail = true;
}
checkFail("removeAll on empty store should return false");
HttpCookie cookie = new HttpCookie("MY_COOKIE", "MY_COOKIE_VALUE");
cookie.setDomain("foo.com");
cookieStore.add(null, cookie);
//Retrieve added cookie
URI uri = new URI("http://foo.com");
List<HttpCookie> addedCookieList = cookieStore.get(uri);
//Verify CookieStore behaves well
if (addedCookieList.size() != 1) {
fail = true;
}
checkFail("Abnormal size of cookie jar");
for (HttpCookie chip : addedCookieList) {
if (!chip.equals(cookie)) {
fail = true;
}
}
checkFail("Cookie not retrieved from Cookie Jar");
boolean ret = cookieStore.remove(null,cookie);
if (!ret) {
fail = true;
}
checkFail("Abnormal removal behaviour from Cookie Jar");
}
static void checkCookieNullUri() throws Exception {
//get a cookie store implementation and add a cookie to the store with null URI
CookieStore cookieStore = (new CookieManager()).getCookieStore();
//Check if removeAll() retrurns false on an empty CookieStore
if (cookieStore.removeAll()) {
fail = true;
}
checkFail("removeAll on empty store should return false");
HttpCookie cookie = new HttpCookie("MY_COOKIE", "MY_COOKIE_VALUE");
cookie.setDomain("foo.com");
cookieStore.add(null, cookie);
//Retrieve added cookie
URI uri = new URI("http://foo.com");
List<HttpCookie> addedCookieList = cookieStore.get(uri);
//Verify CookieStore behaves well
if (addedCookieList.size() != 1) {
fail = true;
}
checkFail("Abnormal size of cookie jar");
for (HttpCookie chip : addedCookieList) {
if (!chip.equals(cookie)) {
fail = true;
}
}
checkFail("Cookie not retrieved from Cookie Jar");
boolean ret = cookieStore.remove(null,cookie);
if (!ret) {
fail = true;
}
checkFail("Abnormal removal behaviour from Cookie Jar");
}
static void checkCookieNullUri() throws Exception {
//get a cookie store implementation and add a cookie to the store with null URI
CookieStore cookieStore = (new CookieManager()).getCookieStore();
//Check if removeAll() retrurns false on an empty CookieStore
if (cookieStore.removeAll()) {
fail = true;
}
checkFail("removeAll on empty store should return false");
HttpCookie cookie = new HttpCookie("MY_COOKIE", "MY_COOKIE_VALUE");
cookie.setDomain("foo.com");
cookieStore.add(null, cookie);
//Retrieve added cookie
URI uri = new URI("http://foo.com");
List<HttpCookie> addedCookieList = cookieStore.get(uri);
//Verify CookieStore behaves well
if (addedCookieList.size() != 1) {
fail = true;
}
checkFail("Abnormal size of cookie jar");
for (HttpCookie chip : addedCookieList) {
if (!chip.equals(cookie)) {
fail = true;
}
}
checkFail("Cookie not retrieved from Cookie Jar");
boolean ret = cookieStore.remove(null,cookie);
if (!ret) {
fail = true;
}
checkFail("Abnormal removal behaviour from Cookie Jar");
}
static void checkCookieNullUri() throws Exception {
//get a cookie store implementation and add a cookie to the store with null URI
CookieStore cookieStore = (new CookieManager()).getCookieStore();
//Check if removeAll() retrurns false on an empty CookieStore
if (cookieStore.removeAll()) {
fail = true;
}
checkFail("removeAll on empty store should return false");
HttpCookie cookie = new HttpCookie("MY_COOKIE", "MY_COOKIE_VALUE");
cookie.setDomain("foo.com");
cookieStore.add(null, cookie);
//Retrieve added cookie
URI uri = new URI("http://foo.com");
List<HttpCookie> addedCookieList = cookieStore.get(uri);
//Verify CookieStore behaves well
if (addedCookieList.size() != 1) {
fail = true;
}
checkFail("Abnormal size of cookie jar");
for (HttpCookie chip : addedCookieList) {
if (!chip.equals(cookie)) {
fail = true;
}
}
checkFail("Cookie not retrieved from Cookie Jar");
boolean ret = cookieStore.remove(null,cookie);
if (!ret) {
fail = true;
}
checkFail("Abnormal removal behaviour from Cookie Jar");
}
static void checkCookieNullUri() throws Exception {
//get a cookie store implementation and add a cookie to the store with null URI
CookieStore cookieStore = (new CookieManager()).getCookieStore();
//Check if removeAll() retrurns false on an empty CookieStore
if (cookieStore.removeAll()) {
fail = true;
}
checkFail("removeAll on empty store should return false");
HttpCookie cookie = new HttpCookie("MY_COOKIE", "MY_COOKIE_VALUE");
cookie.setDomain("foo.com");
cookieStore.add(null, cookie);
//Retrieve added cookie
URI uri = new URI("http://foo.com");
List<HttpCookie> addedCookieList = cookieStore.get(uri);
//Verify CookieStore behaves well
if (addedCookieList.size() != 1) {
fail = true;
}
checkFail("Abnormal size of cookie jar");
for (HttpCookie chip : addedCookieList) {
if (!chip.equals(cookie)) {
fail = true;
}
}
checkFail("Cookie not retrieved from Cookie Jar");
boolean ret = cookieStore.remove(null,cookie);
if (!ret) {
fail = true;
}
checkFail("Abnormal removal behaviour from Cookie Jar");
}
static void checkCookieNullUri() throws Exception {
//get a cookie store implementation and add a cookie to the store with null URI
CookieStore cookieStore = (new CookieManager()).getCookieStore();
//Check if removeAll() retrurns false on an empty CookieStore
if (cookieStore.removeAll()) {
fail = true;
}
checkFail("removeAll on empty store should return false");
HttpCookie cookie = new HttpCookie("MY_COOKIE", "MY_COOKIE_VALUE");
cookie.setDomain("foo.com");
cookieStore.add(null, cookie);
//Retrieve added cookie
URI uri = new URI("http://foo.com");
List<HttpCookie> addedCookieList = cookieStore.get(uri);
//Verify CookieStore behaves well
if (addedCookieList.size() != 1) {
fail = true;
}
checkFail("Abnormal size of cookie jar");
for (HttpCookie chip : addedCookieList) {
if (!chip.equals(cookie)) {
fail = true;
}
}
checkFail("Cookie not retrieved from Cookie Jar");
boolean ret = cookieStore.remove(null,cookie);
if (!ret) {
fail = true;
}
checkFail("Abnormal removal behaviour from Cookie Jar");
}
private static CookieHandler cookieManagerFor(@Nullable File cookieJar) {
CookieStore cookieStore;
try {
cookieStore = cookieJar == null ? null : new FileCookieJar(cookieJar);
} catch (IOException e) {
throw new RuntimeException(e);
}
return new CookieManager(cookieStore, CookiePolicy.ACCEPT_ALL);
}
@Override
public void onUpdate(CookieStore cookieStore) {
super.onUpdate(cookieStore);
int count = totalWrites.incrementAndGet();
if (count % writeCount == 0) {
try {
flush.call();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
@Override
public void onUpdate(CookieStore cookieStore) {
super.onUpdate(cookieStore);
executorService.submit(() -> {
requiresFlush = true;
});
}
/**
* Clears the cookies in the given cookie handler. Cookies can only be cleared if the
* cookieHandler is a CookieManager with a non-null CookieStore.
*
* @param cookieHandler the cookie handler where cookies should be cleared
* @return true if cookies were cleared; false otherwise
*/
public static boolean clearCookies(CookieHandler cookieHandler) {
if (cookieHandler instanceof CookieManager) {
CookieManager cookieManager = (CookieManager) cookieHandler;
CookieStore cookieStore = cookieManager.getCookieStore();
if (cookieStore != null) {
cookieStore.removeAll();
return true;
}
}
return false;
}
static void checkCookieNullUri() throws Exception {
//get a cookie store implementation and add a cookie to the store with null URI
CookieStore cookieStore = (new CookieManager()).getCookieStore();
//Check if removeAll() retrurns false on an empty CookieStore
if (cookieStore.removeAll()) {
fail = true;
}
checkFail("removeAll on empty store should return false");
HttpCookie cookie = new HttpCookie("MY_COOKIE", "MY_COOKIE_VALUE");
cookie.setDomain("foo.com");
cookieStore.add(null, cookie);
//Retrieve added cookie
URI uri = new URI("http://foo.com");
List<HttpCookie> addedCookieList = cookieStore.get(uri);
//Verify CookieStore behaves well
if (addedCookieList.size() != 1) {
fail = true;
}
checkFail("Abnormal size of cookie jar");
for (HttpCookie chip : addedCookieList) {
if (!chip.equals(cookie)) {
fail = true;
}
}
checkFail("Cookie not retrieved from Cookie Jar");
boolean ret = cookieStore.remove(null,cookie);
if (!ret) {
fail = true;
}
checkFail("Abnormal removal behaviour from Cookie Jar");
}
static void checkCookieNullUri() throws Exception {
//get a cookie store implementation and add a cookie to the store with null URI
CookieStore cookieStore = (new CookieManager()).getCookieStore();
//Check if removeAll() retrurns false on an empty CookieStore
if (cookieStore.removeAll()) {
fail = true;
}
checkFail("removeAll on empty store should return false");
HttpCookie cookie = new HttpCookie("MY_COOKIE", "MY_COOKIE_VALUE");
cookie.setDomain("foo.com");
cookieStore.add(null, cookie);
//Retrieve added cookie
URI uri = new URI("http://foo.com");
List<HttpCookie> addedCookieList = cookieStore.get(uri);
//Verify CookieStore behaves well
if (addedCookieList.size() != 1) {
fail = true;
}
checkFail("Abnormal size of cookie jar");
for (HttpCookie chip : addedCookieList) {
if (!chip.equals(cookie)) {
fail = true;
}
}
checkFail("Cookie not retrieved from Cookie Jar");
boolean ret = cookieStore.remove(null,cookie);
if (!ret) {
fail = true;
}
checkFail("Abnormal removal behaviour from Cookie Jar");
}
static void checkCookieNullUri() throws Exception {
//get a cookie store implementation and add a cookie to the store with null URI
CookieStore cookieStore = (new CookieManager()).getCookieStore();
//Check if removeAll() retrurns false on an empty CookieStore
if (cookieStore.removeAll()) {
fail = true;
}
checkFail("removeAll on empty store should return false");
HttpCookie cookie = new HttpCookie("MY_COOKIE", "MY_COOKIE_VALUE");
cookie.setDomain("foo.com");
cookieStore.add(null, cookie);
//Retrieve added cookie
URI uri = new URI("http://foo.com");
List<HttpCookie> addedCookieList = cookieStore.get(uri);
//Verify CookieStore behaves well
if (addedCookieList.size() != 1) {
fail = true;
}
checkFail("Abnormal size of cookie jar");
for (HttpCookie chip : addedCookieList) {
if (!chip.equals(cookie)) {
fail = true;
}
}
checkFail("Cookie not retrieved from Cookie Jar");
boolean ret = cookieStore.remove(null,cookie);
if (!ret) {
fail = true;
}
checkFail("Abnormal removal behaviour from Cookie Jar");
}
static void checkCookieNullUri() throws Exception {
//get a cookie store implementation and add a cookie to the store with null URI
CookieStore cookieStore = (new CookieManager()).getCookieStore();
//Check if removeAll() retrurns false on an empty CookieStore
if (cookieStore.removeAll()) {
fail = true;
}
checkFail("removeAll on empty store should return false");
HttpCookie cookie = new HttpCookie("MY_COOKIE", "MY_COOKIE_VALUE");
cookie.setDomain("foo.com");
cookieStore.add(null, cookie);
//Retrieve added cookie
URI uri = new URI("http://foo.com");
List<HttpCookie> addedCookieList = cookieStore.get(uri);
//Verify CookieStore behaves well
if (addedCookieList.size() != 1) {
fail = true;
}
checkFail("Abnormal size of cookie jar");
for (HttpCookie chip : addedCookieList) {
if (!chip.equals(cookie)) {
fail = true;
}
}
checkFail("Cookie not retrieved from Cookie Jar");
boolean ret = cookieStore.remove(null,cookie);
if (!ret) {
fail = true;
}
checkFail("Abnormal removal behaviour from Cookie Jar");
}
static void checkCookieNullUri() throws Exception {
//get a cookie store implementation and add a cookie to the store with null URI
CookieStore cookieStore = (new CookieManager()).getCookieStore();
//Check if removeAll() retrurns false on an empty CookieStore
if (cookieStore.removeAll()) {
fail = true;
}
checkFail("removeAll on empty store should return false");
HttpCookie cookie = new HttpCookie("MY_COOKIE", "MY_COOKIE_VALUE");
cookie.setDomain("foo.com");
cookieStore.add(null, cookie);
//Retrieve added cookie
URI uri = new URI("http://foo.com");
List<HttpCookie> addedCookieList = cookieStore.get(uri);
//Verify CookieStore behaves well
if (addedCookieList.size() != 1) {
fail = true;
}
checkFail("Abnormal size of cookie jar");
for (HttpCookie chip : addedCookieList) {
if (!chip.equals(cookie)) {
fail = true;
}
}
checkFail("Cookie not retrieved from Cookie Jar");
boolean ret = cookieStore.remove(null,cookie);
if (!ret) {
fail = true;
}
checkFail("Abnormal removal behaviour from Cookie Jar");
}
static void checkCookieNullUri() throws Exception {
//get a cookie store implementation and add a cookie to the store with null URI
CookieStore cookieStore = (new CookieManager()).getCookieStore();
//Check if removeAll() retrurns false on an empty CookieStore
if (cookieStore.removeAll()) {
fail = true;
}
checkFail("removeAll on empty store should return false");
HttpCookie cookie = new HttpCookie("MY_COOKIE", "MY_COOKIE_VALUE");
cookie.setDomain("foo.com");
cookieStore.add(null, cookie);
//Retrieve added cookie
URI uri = new URI("http://foo.com");
List<HttpCookie> addedCookieList = cookieStore.get(uri);
//Verify CookieStore behaves well
if (addedCookieList.size() != 1) {
fail = true;
}
checkFail("Abnormal size of cookie jar");
for (HttpCookie chip : addedCookieList) {
if (!chip.equals(cookie)) {
fail = true;
}
}
checkFail("Cookie not retrieved from Cookie Jar");
boolean ret = cookieStore.remove(null,cookie);
if (!ret) {
fail = true;
}
checkFail("Abnormal removal behaviour from Cookie Jar");
}
public Builder enableCookies(CookieStore cookieStore, CookiePolicy cookiePolicy) {
if (cookiePolicy == null) {
throw new NullPointerException("CookiePolicy may not be null");
}
this.cookieHandler = new CookieManager(cookieStore, cookiePolicy);
return this;
}
public void testCookieStoreRemoveAll() throws URISyntaxException {
CookieStore cookieStore = new CookieManager(createCookieStore(), null).getCookieStore();
cookieStore.add(new URI("http://code.google.com/"), new HttpCookie("a", "android"));
assertTrue(cookieStore.removeAll());
assertEquals(Collections.<URI>emptyList(), cookieStore.getURIs());
assertEquals(Collections.<HttpCookie>emptyList(), cookieStore.getCookies());
assertFalse("Expected removeAll() to return false when the call doesn't mutate the store",
cookieStore.removeAll()); // RI6 fails this
}
public void testCookieStoreRemoveRequiresUri() throws URISyntaxException {
CookieStore cookieStore = new CookieManager(createCookieStore(), null).getCookieStore();
HttpCookie cookieA = new HttpCookie("a", "android");
cookieStore.add(new URI("http://android.com/source/"), cookieA);
assertFalse("Expected remove() to take the cookie URI into account.", // RI6 fails this
cookieStore.remove(new URI("http://code.google.com/"), cookieA));
assertEquals(Arrays.asList(cookieA), cookieStore.getCookies());
}
public void testCookieStoreGet() throws Exception {
CookieStore cookieStore = new CookieManager(createCookieStore(), null).getCookieStore();
HttpCookie cookiePort1 = createCookie("a1", "android", "a.com", "/path1");
HttpCookie cookiePort2 = createCookie("a2", "android", "a.com", "/path2");
HttpCookie secureCookie = createCookie("a3", "android", "a.com", "/path3");
secureCookie.setSecure(true);
HttpCookie notSecureCookie = createCookie("a4", "android", "a.com", "/path4");
HttpCookie bCookie = createCookie("b1", "android", "b.com", "/path5");
cookieStore.add(new URI("http://a.com:443/path1"), cookiePort1);
cookieStore.add(new URI("http://a.com:8080/path2"), cookiePort2);
cookieStore.add(new URI("https://a.com:443/path3"), secureCookie);
cookieStore.add(new URI("https://a.com:443/path4"), notSecureCookie);
cookieStore.add(new URI("https://b.com:8080/path5"), bCookie);
List<HttpCookie> expectedStoreCookies = new ArrayList<>();
expectedStoreCookies.add(cookiePort1);
expectedStoreCookies.add(cookiePort2);
expectedStoreCookies.add(secureCookie);
expectedStoreCookies.add(notSecureCookie);
// The default CookieStore implementation on Android is currently responsible for matching
// the host/domain but not handling other cookie rules: it ignores the scheme (e.g. "secure"
// checks), port and path.
// The tests below fail on the RI. It looks like in the RI it is CookieStoreImpl that is
// enforcing "secure" checks.
assertEquals(expectedStoreCookies, cookieStore.get(new URI("http://a.com:443/anypath")));
assertEquals(expectedStoreCookies, cookieStore.get(new URI("http://a.com:8080/anypath")));
assertEquals(expectedStoreCookies, cookieStore.get(new URI("https://a.com/anypath")));
assertEquals(expectedStoreCookies, cookieStore.get(new URI("http://a.com/anypath")));
}
/**
* Regression test for http://b/25682357 /
* https://code.google.com/p/android/issues/detail?id=193475
* CookieStoreImpl.get(URI) not handling ports properly in the absence of an explicit cookie
* Domain.
*/
public void testCookieStoreGetWithPort() throws Exception {
CookieStore cookieStore = new CookieManager(createCookieStore(), null).getCookieStore();
HttpCookie cookie = new HttpCookie("theme", "light");
// Deliberately not setting the cookie domain or path.
cookieStore.add(new URI("http://a.com:12345"), cookie);
// CookieStoreImpl must ignore the port during retrieval when domain is not set.
assertEquals(1, cookieStore.get(new URI("http://a.com:12345/path1")).size());
assertEquals(1, cookieStore.get(new URI("http://a.com/path1")).size());
}
public void testCookieStoreGetWithSecure() throws Exception {
CookieStore cookieStore = new CookieManager(createCookieStore(), null).getCookieStore();
HttpCookie cookie = createCookie("theme", "light", "a.com", "/path");
cookie.setSecure(true);
cookieStore.add(new URI("https://a.com/path"), cookie);
// CookieStoreImpl on Android ignores the "Secure" attribute. The RI implements the secure
// check in CookieStoreImpl. For safety / app compatibility, if this is changed Android
// should probably implement it in both places.
assertEquals(1, cookieStore.get(new URI("http://a.com/path")).size());
assertEquals(1, cookieStore.get(new URI("https://a.com/path")).size());
}
public void testCookieStoreEviction() throws Exception {
CookieStore cookieStore = new CookieManager(createCookieStore(), null).getCookieStore();
HttpCookie themeCookie = createCookie("theme", "light", "a.com", "/");
cookieStore.add(new URI("http://a.com/"), themeCookie);
HttpCookie sidCookie = createCookie("sid", "mysid", "a.com", "/");
cookieStore.add(new URI("http://a.com/"), sidCookie);
HttpCookie replacementThemeCookie = createCookie("theme", "dark", "a.com", "/");
cookieStore.add(new URI("http://a.com/"), replacementThemeCookie);
// toString() is used below to avoid confusion with assertEquals():
// HttpCookie.equals() is implemented so that it only checks name, path and domain
// attributes but we also want to check the value.
assertEquals(
"[sid=\"mysid\";$Path=\"/\";$Domain=\"a.com\", "
+ "theme=\"dark\";$Path=\"/\";$Domain=\"a.com\"]",
cookieStore.get(new URI("http://a.com/")).toString());
HttpCookie replacementSidCookie = createCookie("sid", "mynewsid", "A.cOm", "/");
cookieStore.add(new URI("http://a.com/"), replacementSidCookie);
assertEquals(
"[theme=\"dark\";$Path=\"/\";$Domain=\"a.com\", "
+ "sid=\"mynewsid\";$Path=\"/\";$Domain=\"a.com\"]",
cookieStore.get(new URI("http://a.com/")).toString());
}
public void logoutAll() {
for (AuthCookie authCookie : authCookies.values()) {
authCookie.logout();
}
CookieStore cookies = ((CookieManager) CookieHandler.getDefault()).getCookieStore();
cookies.removeAll();
}
@Override
public CookieStore getCookieStore() {
// if (cookieManager == null) // No special cookie manager - use system default
return super.getCookieStore();
// we don't want anyone to work with this cookie store directly
}