/** * @throws CloneNotSupportedException * @see java.lang.Object#clone() */ @Override public Cookie clone() throws CloneNotSupportedException { Cookie ret = (Cookie) super.clone(); ret.domain = this.domain; ret.name = this.name; ret.value = this.value; ret.path = this.path; ret.max_age = this.max_age; ret.version = this.version; ret.secure = this.secure; return ret; }
public void addCookie(String key, String val, String host, String path) { Cookie c = new Cookie(key, val, host, path); // 设置Cookie String name = c.name(); String value = c.value(); List<String> vals = this.cookies.get(name); if (vals == null) vals = new ArrayList<String>(); vals.add(value); this.cookies.put(key, vals); BasicClientCookie clientCookie = new BasicClientCookie(name, value); clientCookie.setPath(c.path()); clientCookie.setDomain(c.domain()); httpClient.getCookieStore().addCookie(clientCookie); }
private void processCookies(WebSocketResponse respone) { Header[] headers = respone.getHeaders(); for (Header header : headers) { if (header.getName().equals("Set-Cookie")) { String cookies = header.getValue(); if (cookies == null || cookies.isEmpty()) { return; } String[] split = cookies.split(";"); String[] keyValues = split[0].split("="); Cookie cookie = new Cookie(); cookie.name = keyValues[0]; cookie.value = keyValues[1]; int contains = isContains(cookiesList, cookie.name); if (contains > 0) { cookiesList.remove(contains); } cookiesList.add(cookie); } } }
/** * Parses a "Set-Cookie" header and creates/updates/deletes cookies according to the parsed * values. Parsing is done according to the specification in RFC 2109 * * @param s The plain value of the "Set-Cookie" HTTP header. e.g.: without the "Set-Cookie: " * prefix. */ public void parseCookieHeader(String s) { StringTokenizer t = new StringTokenizer(s, ";"); Cookie cookie = new Cookie(); while (t.hasMoreTokens()) { Matcher m = pNvPair.matcher(t.nextToken()); if (m.matches()) { String n = m.group(1); String v = (m.groupCount() > 2 && m.group(3) != null) ? m.group(3).trim() : ""; if (n.compareToIgnoreCase("version") == 0) { cookie.version = Integer.parseInt(v); continue; } if (n.compareToIgnoreCase("domain") == 0) { cookie.domain = v; continue; } if (n.compareToIgnoreCase("path") == 0) { cookie.path = v; continue; } if (n.compareToIgnoreCase("max-age") == 0) { cookie.max_age = Integer.parseInt(v); if (cookie.max_age < 0) cookie.max_age = 0; cookie.max_age *= 1000; cookie.max_age += System.currentTimeMillis(); continue; } if (n.compareToIgnoreCase("expires") == 0) { SimpleDateFormat df = new SimpleDateFormat("EEE, dd-MMM-yy HH:mm:ss zzz"); try { cookie.max_age = System.currentTimeMillis() - df.parse(v).getTime(); if (cookie.max_age < 0) cookie.max_age = 0; } catch (ParseException e) { cookie.max_age = 0; } continue; } if (n.compareToIgnoreCase("comment") == 0) { // ignored continue; } if (n.compareToIgnoreCase("secure") == 0) { cookie.secure = 1; continue; } if (!n.startsWith("$")) { if (null != cookie.name) { if (cookie.version > 0) { // Strip possible quotes cookie.domain = stripQuotes(cookie.domain); cookie.path = stripQuotes(cookie.path); // cookie.comment = stripQuotes(cookie.comment); // cookie.name = stripQuotes(cookie.name); // cookie.value = stripQuotes(cookie.value); } if (domainMatch(cookie.domain)) { if (cookie.max_age > 0) { this.jar.put(cookie.getKey(), cookie); this.uploadPolicy.displayDebug( "[CookieJar] Adding cookie: " + cookie.getKey() + ": " + cookie, 50); } else { this.jar.put(cookie.getKey(), null); this.uploadPolicy.displayDebug( "[CookieJar] Ignoring cookie: " + cookie.getKey() + ": " + cookie, 50); } } try { cookie = cookie.clone(); } catch (CloneNotSupportedException e) { cookie = new Cookie(); } } cookie.name = n; cookie.value = v; } } } }