如何解析cookie字符串 [英] How to parse a cookie string
问题描述
我想要一个 Cookie 字符串(因为它可能会在 Set-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.
我看到有几个不同的 Cookie 类,例如 BasicClientCookie,但我没有看到任何简单的方法可以将字符串解析为这些对象之一.
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.
我在 api 级别 9 中看到他们添加了 HttpCookieparse 方法,但我需要一些东西在以前的版本中工作.
I see in api level 9 they added HttpCookie which has a parse method, but I need something to work in previous versions.
有什么想法吗?
谢谢
推荐答案
我相信您必须手动解析它.试试这个:
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;
}
我还没有真正编译或运行过这段代码,但这应该是一个很好的开始.您可能不得不对日期解析有些困惑:我不确定 cookie 中使用的日期格式实际上是否与 DateFormat.FULL
相同.(查看 this 相关问题,该问题解决了处理 cookie 中的日期格式.)另外,请注意有一些 cookie 属性不由 BasicClientCookie
处理,例如 version
和 httponly
.
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
.
最后,这段代码假设 cookie 的名称和值作为第一个属性出现:我不确定这是否一定是真的,但我见过的每个 cookie 都是这样排序的.
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屋!