Cookies with expiration date set from java not handled?

Having problems with building or using the JCEF Java binding? Ask your questions here.

Cookies with expiration date set from java not handled?

Postby JohnnyTheHun » 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:

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;
        }
    }
Last edited by JohnnyTheHun on Fri Jan 08, 2021 8:19 pm, edited 1 time in total.
JohnnyTheHun
Techie
 
Posts: 42
Joined: Tue Apr 10, 2018 12:24 pm

Re: Cookies with expiration date in the future are not handl

Postby JohnnyTheHun » Fri Jan 08, 2021 7:46 pm

I also tried this in the detailed sample app:
1. I created a test cookie in the sample detailed application in file menu / show cookies. The test cookie has an expiration date.
2. I closed this show cookies window
3. I opened the file menu / show cookies again.
4. The test cookie I created disappeared.

Can anyone else try the sample app from the current version if it's showing the same behaviour?
JohnnyTheHun
Techie
 
Posts: 42
Joined: Tue Apr 10, 2018 12:24 pm

Re: Cookies with expiration date set from java not handled?

Postby smartBeaver » Mon Aug 02, 2021 12:33 pm

I had the same problem where the setCookie would return true but the cookies could not be seen by the CookieVisitor. Setting the hasExpiry to false like you mentionned solved my problem and I wanted to thank you :D.
smartBeaver
Techie
 
Posts: 10
Joined: Wed May 26, 2021 7:51 am


Return to JCEF Forum

Who is online

Users browsing this forum: No registered users and 7 guests