下面列出了怎么用org.apache.http.cookie.SetCookie的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* Change cookie value.
* If several cookies with the same name exist in cookie store, the value will be changed for all of them,
* @param cookieName name of cookie
* @param newCookieValue value to set
*/
@When("I change value of all HTTP cookies with name `$cookieName` to `$newCookieValue`")
public void changeHttpCookieValue(String cookieName, String newCookieValue)
{
List<Cookie> cookies = findCookiesBy(FILTER_BY_NAME, cookieName);
if (assertCookiesPresent(cookieName, cookies.size()))
{
cookies.forEach(cookie -> {
if (cookie instanceof SetCookie)
{
((SetCookie) cookie).setValue(newCookieValue);
}
else
{
throw new IllegalStateException(
String.format("Unable to change value of cookie with name '%s' of type '%s'", cookieName,
cookie.getClass().getName()));
}
});
}
}
@Override
public void parse(final SetCookie cookie, final String value)
throws MalformedCookieException {
Args.notNull(cookie, HttpHeader.COOKIE);
if (TextUtils.isBlank(value)) {
throw new MalformedCookieException("Blank or null value for domain attribute");
}
// Ignore domain attributes ending with '.' per RFC 6265, 4.1.2.3
if (value.endsWith(".")) {
return;
}
String domain = value;
domain = domain.toLowerCase(Locale.ROOT);
final int dotIndex = domain.indexOf('.');
if (browserVersion_.hasFeature(HTTP_COOKIE_REMOVE_DOT_FROM_ROOT_DOMAINS)
&& dotIndex == 0 && domain.length() > 1 && domain.indexOf('.', 1) == -1) {
domain = domain.toLowerCase(Locale.ROOT);
domain = domain.substring(1);
}
if (dotIndex > 0) {
domain = '.' + domain;
}
cookie.setDomain(domain);
}
/**
* Parse cookie version attribute.
*/
@Override
public void parse(final SetCookie cookie, final String value) throws MalformedCookieException {
if (value == null) {
throw new MalformedCookieException("Missing value for version attribute");
}
int version = 0;
try {
version = Integer.parseInt(value);
}
catch (final NumberFormatException e) {
// ignore invalid versions
}
cookie.setVersion(version);
}
@Override
public void parse(final SetCookie cookie, String value) throws MalformedCookieException {
if (value.startsWith("\"") && value.endsWith("\"")) {
value = value.substring(1, value.length() - 1);
}
value = value.replaceAll("[ ,:-]+", " ");
Date startDate = null;
String[] datePatterns = DEFAULT_DATE_PATTERNS;
if (null != browserVersion_) {
if (browserVersion_.hasFeature(HTTP_COOKIE_START_DATE_1970)) {
startDate = HtmlUnitBrowserCompatCookieSpec.DATE_1_1_1970;
}
if (browserVersion_.hasFeature(HTTP_COOKIE_EXTENDED_DATE_PATTERNS_1)) {
datePatterns = EXTENDED_DATE_PATTERNS_1;
}
if (browserVersion_.hasFeature(HTTP_COOKIE_EXTENDED_DATE_PATTERNS_2)) {
final Calendar calendar = Calendar.getInstance(Locale.ROOT);
calendar.setTimeZone(DateUtils.GMT);
calendar.set(1969, Calendar.JANUARY, 1, 0, 0, 0);
calendar.set(Calendar.MILLISECOND, 0);
startDate = calendar.getTime();
datePatterns = EXTENDED_DATE_PATTERNS_2;
}
}
final Date expiry = DateUtils.parseDate(value, datePatterns, startDate);
cookie.setExpiryDate(expiry);
}
@Override
public void parse(final SetCookie cookie, final String value) throws MalformedCookieException {
((BasicClientCookie) cookie).setAttribute(HTTPONLY_ATTR, "true");
}
public void parse(final SetCookie cookie, final String value) throws MalformedCookieException {
Args.notNull(cookie, "Cookie");
((BasicClientCookie) cookie).setAttribute(CookieUtil.HTTP_ONLY_ATTR, "");
}