Cookies with expiration date set from java not handled?
Posted: Fri Jan 08, 2021 5:26 am
I am setting cookies programmatically through java. I want some cookies to persist so I set their expiration date. What I have noticed is that visiting cookies do not show those that have their expiration date set so I assume they get thrown away. If I save the same cookie but set hasExpires to false it will appear when visiting cookies.
JCEF Version = 87.1.12.275
CEF Version = 87.1.12
Chromium Version = 87.0.4280.88
I might have not noticed something trivial or this might be a bug?
The code for cookie testing:
JCEF Version = 87.1.12.275
CEF Version = 87.1.12
Chromium Version = 87.0.4280.88
I might have not noticed something trivial or this might be a bug?
The code for cookie testing:
- Code: Select all
private void testCookie() {
//CefCookieManager cm = requestContext.getCookieManager();
CefCookieManager cm = CefCookieManager.getGlobalManager();
if(cm!=null) {
try {
String url = browser_.getURL(); //browser_ is the CefBrowser
URL urlUrl = new URL(url);
String urlForCookie = urlUrl.getProtocol()+"://"+urlUrl.getHost();
if(urlUrl.getPort()>0) urlForCookie += ":"+urlUrl.getPort();
CefCookie cc = createCookie(urlUrl);
boolean ret = cm.setCookie(urlForCookie, cc);
System.out.println("Adding cookie: "+cc.name+" = "+cc.value+" "
+ "expires:"+(cc.hasExpires?java.text.DateFormat.getDateTimeInstance().format(cc.expires):"NO")+" returned:"+ret
);
} catch(Exception e) {
System.out.println("Unable to add cookie");
e.printStackTrace(System.out);
}
System.out.println("Visiting cookies: ");
cm.visitAllCookies(new CefCookieVisitor() {
@Override
public boolean visit(CefCookie cc, int i, int i1, BoolRef br) {
System.out.println(cc.name+" = "+cc.value);
return true;
}
});
} else {
System.out.println("Cookiemanager is NULL");
}
}
CefCookie createCookie(URL cookieUrl) {
try {
String domain = cookieUrl.getHost();
String cookieName = "TestCookie_"+(int)(Math.random()*1000);
String cookieValue = System.currentTimeMillis()+"";
/* Cookie params:
1: java.lang.String name,
2: java.lang.String value,
3: java.lang.String domain,
4: java.lang.String path,
5: boolean secure,
6: boolean httponly,
7: java.util.Date creation,
8: java.util.Date lastAccess,
9: boolean hasExpires,
10: java.util.Date expires
*/
Date expires = new GregorianCalendar(2022,0,15,0,1).getTime();
CefCookie ret = new CefCookie(
cookieName,
cookieValue,
domain,
"/",
false,
false,
new java.util.Date(),
new java.util.Date(),
true, //if I change this to false the cookie visitor will find the cookie
expires
);
return ret;
} catch(Exception e) {
e.printStackTrace(System.out);
return null;
}
}