How to parse a cookie string

后端 未结 9 1853
眼角桃花
眼角桃花 2020-12-08 04:47

I would like to take a Cookie string (as it might be returned in a Set-Cookie header) and be able to easily modify parts of it, specifically the expiration date.

I s

相关标签:
9条回答
  • 2020-12-08 05:11

    How about java.net.HttpCookie:

    List<HttpCookie> cookies = HttpCookie.parse(header);
    
    0 讨论(0)
  • 2020-12-08 05:12

    With a regular expression like :

    ([^=]+)=([^\;]+);\s?
    

    you can parse a cookie like this :

    .COOKIEAUTH=5DEF0BF530F749AD46F652BDF31C372526A42FEB9D40162167CB39C4D43FC8AF1C4B6DF0C24ECB1945DFF7952C70FDA1E4AF12C1803F9D089E78348C4B41802279897807F85905D6B6D2D42896BA2A267E9F564814631B4B31EE41A483C886B14B5A1E76FD264FB230E87877CB9A4A2A7BDB0B0101BC2C1AF3A029CC54EE4FBC; 
    expires=Sat, 30-Jul-2011 01:22:34 GMT; 
    path=/; HttpOnly
    

    in a few lines of code.

    0 讨论(0)
  • 2020-12-08 05:15

    I believe you'll have to parse it out manually. Try this:

    BasicClientCookie parseRawCookie(String rawCookie) throws Exception {
        String[] rawCookieParams = rawCookie.split(";");
    
        String[] rawCookieNameAndValue = rawCookieParams[0].split("=");
        if (rawCookieNameAndValue.length != 2) {
            throw new Exception("Invalid cookie: missing name and value.");
        }
    
        String cookieName = rawCookieNameAndValue[0].trim();
        String cookieValue = rawCookieNameAndValue[1].trim();
        BasicClientCookie cookie = new BasicClientCookie(cookieName, cookieValue);
        for (int i = 1; i < rawCookieParams.length; i++) {
            String rawCookieParamNameAndValue[] = rawCookieParams[i].trim().split("=");
    
            String paramName = rawCookieParamNameAndValue[0].trim();
    
            if (paramName.equalsIgnoreCase("secure")) {
                cookie.setSecure(true);
            } else {
                if (rawCookieParamNameAndValue.length != 2) {
                    throw new Exception("Invalid cookie: attribute not a flag or missing value.");
                }
    
                String paramValue = rawCookieParamNameAndValue[1].trim();
    
                if (paramName.equalsIgnoreCase("expires")) {
                    Date expiryDate = DateFormat.getDateTimeInstance(DateFormat.FULL)
                            .parse(paramValue);
                    cookie.setExpiryDate(expiryDate);
                } else if (paramName.equalsIgnoreCase("max-age")) {
                    long maxAge = Long.parseLong(paramValue);
                    Date expiryDate = new Date(System.getCurrentTimeMillis() + maxAge);
                    cookie.setExpiryDate(expiryDate);
                } else if (paramName.equalsIgnoreCase("domain")) {
                    cookie.setDomain(paramValue);
                } else if (paramName.equalsIgnoreCase("path")) {
                    cookie.setPath(paramValue);
                } else if (paramName.equalsIgnoreCase("comment")) {
                    cookie.setPath(paramValue);
                } else {
                    throw new Exception("Invalid cookie: invalid attribute name.");
                }
            }
        }
    
        return cookie;
    }
    

    I haven't actually compiled or run this code, but it should be a strong start. You'll probably have to mess with the date parsing a bit: I'm not sure that the date format used in cookies is actually the same as DateFormat.FULL. (Check out this related question, which addresses handling the date format in cookies.) Also, note that there are some cookie attributes not handled by BasicClientCookie such as version and httponly.

    Finally, this code assumes that the name and value of the cookie appear as the first attribute: I'm not sure if that's necessarily true, but that's how every cookie I've ever seen is ordered.

    0 讨论(0)
提交回复
热议问题