public static String generateSetCookie(Cookie cookie) { if (cookie == null) { throw new IllegalArgumentException("the cookie is null"); } else { StringBuilder sb = new StringBuilder(); sb.append(cookie.getName()).append('=').append(cookie.getValue()); if (VerifyUtils.isNotEmpty(cookie.getComment())) { sb.append(";Comment=").append(cookie.getComment()); } if (VerifyUtils.isNotEmpty(cookie.getDomain())) { sb.append(";Domain=").append(cookie.getDomain()); } if (cookie.getMaxAge() >= 0) { sb.append(";Max-Age=").append(cookie.getMaxAge()); } String path = VerifyUtils.isEmpty(cookie.getPath()) ? "/" : cookie.getPath(); sb.append(";Path=").append(path); if (cookie.getSecure()) { sb.append(";Secure"); } sb.append(";Version=").append(cookie.getVersion()); return sb.toString(); } }
@Override public Cookie[] getCookies() { if (cookies == null) { List<Cookie> list = new ArrayList<Cookie>(); String cookieStr = getHeader("Cookie"); if (VerifyUtils.isEmpty(cookieStr)) { cookies = EMPTY_COOKIE_ARR; } else { String[] c = StringUtils.split(cookieStr, ';'); for (String t : c) { int j = 0; for (int i = 0; i < t.length(); i++) { if (t.charAt(i) == '=') { j = i; break; } } if (j > 1) { String name = t.substring(0, j).trim(); String value = t.substring(j + 1).trim(); Cookie cookie = new Cookie(name, value); list.add(cookie); } else continue; } cookies = list.toArray(EMPTY_COOKIE_ARR); } } return cookies; }