如何分析一个cookie字符串 [英] How to parse a cookie string

查看:101
本文介绍了如何分析一个cookie字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想借一个cookie字符串(因为它可能会在一个Set-Cookie头被退回),并能够方便地修改它的部分,具体的到期日期。

我看到有几个不同的饼干类,如BasicClientCookie,可用,但我看不出有什么简单的方法来解析字符串到这些对象之一。

我在API层面看9,他们加入的HttpCookie 其中有一个分析方法,但我需要的东西在previous版本一起使用。

任何想法?

感谢

解决方案

我认为你必须手动解析出来。试试这个:

  BasicClientCookie parseRawCookie(字符串rawCookie)抛出异常{
    的String [] rawCookieParams = rawCookie.split();

    串[] rawCookieNameAndValue = rawCookieParams [0] .split(=);
    如果(rawCookieNameAndValue.length!= 2){
        抛出新的异常(无效的cookie:缺少名称和值);
    }

    串cookieName = rawCookieNameAndValue [0] .trim();
    串cookieValue = rawCookieNameAndValue [1] .trim();
    BasicClientCookie饼干=新BasicClientCookie(cookieName,cookieValue);
    的for(int i = 1; I< rawCookieParams.length;我++){
        。字符串rawCookieParamNameAndValue [] = rawCookieParams [I] .trim()分裂(=);

        串paramName配置= rawCookieParamNameAndValue [0] .trim();

        如果(paramName.equalsIgnoreCase(安全)){
            cookie.setSecure(真正的);
        } 其他 {
            如果(rawCookieParamNameAndValue.length!= 2){
                抛出新的异常:(无效的Cookie的属性没有标志或缺失值。)
            }

            串paramValue = rawCookieParamNameAndValue [1] .trim();

            如果(paramName.equalsIgnoreCase(过期)){
                日期expiryDate = DateFormat.getDateTimeInstance(DateFormat.FULL)
                        .parse(paramValue);
                cookie.setExpiryDate(expiryDate);
            }否则,如果(paramName.equalsIgnoreCase(最大年龄)){
                长的最大生存周期=的Long.parseLong(paramValue);
                日期expiryDate =新的日期(System.getCurrentTimeMillis()+最大生存周期);
                cookie.setExpiryDate(expiryDate);
            }否则,如果(paramName.equalsIgnoreCase(域)){
                cookie.setDomain(paramValue);
            }否则,如果(paramName.equalsIgnoreCase(路径)){
                cookie.setPath(paramValue);
            }否则,如果(paramName.equalsIgnoreCase(意见)){
                cookie.setPath(paramValue);
            } 其他 {
                抛出新的异常(无效的Cookie:无效的属性名。);
            }
        }
    }

    返回的cookie;
}
 

我还没有实际编译或运行此code,但它应该是一个良好的开端。你可能要惹日期解析了一下:我不知道,在饼干中使用的日期格式实际上是一样的 DateFormat.FULL 。 (请查看这个的相关的问题,其中涉及处理日期格式的cookies。)另外,请注意,有一些cookie的属性不是由 BasicClientCookie 处理,如版本仅Http

最后,code假定cookie的名称和值出现的第一个属性:我不知道这是完全正确,但是这就是我见过的每一个cookie被责令

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 see there are several different Cookie classes, such as BasicClientCookie, available but I don't see any easy way to parse the string into one of those objects.

I see in api level 9 they added HttpCookie which has a parse method, but I need something to work in previous versions.

Any ideas?

Thanks

解决方案

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.

这篇关于如何分析一个cookie字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆