本文整理匯總了Java中org.apache.commons.httpclient.Cookie.setExpiryDate方法的典型用法代碼示例。如果您正苦於以下問題:Java Cookie.setExpiryDate方法的具體用法?Java Cookie.setExpiryDate怎麽用?Java Cookie.setExpiryDate使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.apache.commons.httpclient.Cookie
的用法示例。
在下文中一共展示了Cookie.setExpiryDate方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: parse
import org.apache.commons.httpclient.Cookie; //導入方法依賴的package包/類
/**
* Parse cookie max-age attribute.
*/
public void parse(final Cookie cookie, final String value)
throws MalformedCookieException {
if (cookie == null) {
throw new IllegalArgumentException("Cookie may not be null");
}
if (value == null) {
throw new MalformedCookieException(
"Missing value for max-age attribute");
}
int age = -1;
try {
age = Integer.parseInt(value);
} catch (NumberFormatException e) {
age = -1;
}
if (age < 0) {
throw new MalformedCookieException ("Invalid max-age attribute.");
}
cookie.setExpiryDate(new Date(System.currentTimeMillis() + age * 1000L));
}
示例2: parseAttribute
import org.apache.commons.httpclient.Cookie; //導入方法依賴的package包/類
/**
* Parse the cookie attribute and update the corresponsing {@link Cookie}
* properties as defined by the Netscape draft specification
*
* @param attribute {@link NameValuePair} cookie attribute from the
* <tt>Set- Cookie</tt>
* @param cookie {@link Cookie} to be updated
* @throws MalformedCookieException if an exception occurs during parsing
*/
public void parseAttribute(
final NameValuePair attribute, final Cookie cookie)
throws MalformedCookieException {
if (attribute == null) {
throw new IllegalArgumentException("Attribute may not be null.");
}
if (cookie == null) {
throw new IllegalArgumentException("Cookie may not be null.");
}
final String paramName = attribute.getName().toLowerCase();
final String paramValue = attribute.getValue();
if (paramName.equals("expires")) {
if (paramValue == null) {
throw new MalformedCookieException(
"Missing value for expires attribute");
}
try {
DateFormat expiryFormat = new SimpleDateFormat(
"EEE, dd-MMM-yyyy HH:mm:ss z", Locale.US);
Date date = expiryFormat.parse(paramValue);
cookie.setExpiryDate(date);
} catch (ParseException e) {
throw new MalformedCookieException("Invalid expires "
+ "attribute: " + e.getMessage());
}
} else {
super.parseAttribute(attribute, cookie);
}
}