本文整理匯總了Java中org.apache.commons.httpclient.Cookie類的典型用法代碼示例。如果您正苦於以下問題:Java Cookie類的具體用法?Java Cookie怎麽用?Java Cookie使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
Cookie類屬於org.apache.commons.httpclient包,在下文中一共展示了Cookie類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: testParseSimple
import org.apache.commons.httpclient.Cookie; //導入依賴的package包/類
public void testParseSimple() throws Exception {
Header header = new Header("Set-Cookie","cookie-name=cookie-value");
CookieSpec cookiespec = new CookieSpecBase();
Cookie[] parsed = cookieParse(cookiespec, "127.0.0.1", 80, "/path/path", false, header);
assertEquals("Found 1 cookie.",1,parsed.length);
assertEquals("Name","cookie-name",parsed[0].getName());
assertEquals("Value","cookie-value",parsed[0].getValue());
assertTrue("Comment",null == parsed[0].getComment());
assertTrue("ExpiryDate",null == parsed[0].getExpiryDate());
//assertTrue("isToBeDiscarded",parsed[0].isToBeDiscarded());
assertTrue("isPersistent",!parsed[0].isPersistent());
assertEquals("Domain","127.0.0.1",parsed[0].getDomain());
assertEquals("Path","/path",parsed[0].getPath());
assertTrue("Secure",!parsed[0].getSecure());
assertEquals("Version",0,parsed[0].getVersion());
}
示例2: testKeepCloverHappy
import org.apache.commons.httpclient.Cookie; //導入依賴的package包/類
public void testKeepCloverHappy() throws Exception {
CookieSpec cookiespec = new IgnoreCookiesSpec();
cookiespec.parseAttribute(null, null);
cookiespec.parse("host", 80, "/", false, (String)null);
cookiespec.parse("host", 80, "/", false, (Header)null);
cookiespec.validate("host", 80, "/", false, (Cookie)null);
cookiespec.match("host", 80, "/", false, (Cookie)null);
cookiespec.match("host", 80, "/", false, (Cookie [])null);
cookiespec.domainMatch(null, null);
cookiespec.pathMatch(null, null);
cookiespec.match("host", 80, "/", false, (Cookie [])null);
cookiespec.formatCookie(null);
cookiespec.formatCookies(null);
cookiespec.formatCookieHeader((Cookie)null);
cookiespec.formatCookieHeader((Cookie [])null);
}
示例3: testSetCookieVersionMix
import org.apache.commons.httpclient.Cookie; //導入依賴的package包/類
public void testSetCookieVersionMix() throws IOException {
this.server.setHttpService(new SetCookieVersionMixService());
this.client.setState(new TestHttpState());
this.client.getParams().setCookiePolicy(CookiePolicy.RFC_2965);
GetMethod httpget1 = new GetMethod("/test/");
try {
this.client.executeMethod(httpget1);
} finally {
httpget1.releaseConnection();
}
Cookie[] cookies = this.client.getState().getCookies();
assertNotNull(cookies);
assertEquals(1, cookies.length);
assertEquals("right", cookies[0].getValue());
assertTrue(cookies[0] instanceof Cookie2);
}
示例4: parseAttribute
import org.apache.commons.httpclient.Cookie; //導入依賴的package包/類
/**
* Parse RFC 2965 specific cookie attribute and update the corresponsing
* {@link org.apache.commons.httpclient.Cookie} properties.
*
* @param attribute {@link org.apache.commons.httpclient.NameValuePair} cookie attribute from the
* <tt>Set-Cookie2</tt> header.
* @param cookie {@link org.apache.commons.httpclient.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 (attribute.getName() == null) {
throw new IllegalArgumentException("Attribute Name 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();
CookieAttributeHandler handler = findAttribHandler(paramName);
if (handler == null) {
// ignore unknown attribute-value pairs
if (LOG.isDebugEnabled())
LOG.debug("Unrecognized cookie attribute: " +
attribute.toString());
} else {
handler.parse(cookie, paramValue);
}
}
示例5: parse
import org.apache.commons.httpclient.Cookie; //導入依賴的package包/類
/**
* Parse cookie path attribute.
*/
public void parse(final Cookie cookie, final String path)
throws MalformedCookieException {
if (cookie == null) {
throw new IllegalArgumentException("Cookie may not be null");
}
if (path == null) {
throw new MalformedCookieException(
"Missing value for path attribute");
}
if (path.trim().equals("")) {
throw new MalformedCookieException(
"Blank value for path attribute");
}
cookie.setPath(path);
cookie.setPathAttributeSpecified(true);
}
示例6: match
import org.apache.commons.httpclient.Cookie; //導入依賴的package包/類
/**
* Match cookie path attribute. The value for the Path attribute must be a
* prefix of the request-URI (case-sensitive matching).
*/
public boolean match(final Cookie cookie, final CookieOrigin origin) {
if (cookie == null) {
throw new IllegalArgumentException("Cookie may not be null");
}
if (origin == null) {
throw new IllegalArgumentException("Cookie origin may not be null");
}
String path = origin.getPath();
if (cookie.getPath() == null) {
LOG.warn("Invalid cookie state: path attribute is null.");
return false;
}
if (path.trim().equals("")) {
path = PATH_DELIM;
}
if (!pathMatch(path, cookie.getPath())) {
return false;
}
return true;
}
示例7: validate
import org.apache.commons.httpclient.Cookie; //導入依賴的package包/類
/**
* Validate cookie port attribute. If the Port attribute was specified
* in header, the request port must be in cookie's port list.
*/
public void validate(final Cookie cookie, final CookieOrigin origin)
throws MalformedCookieException {
if (cookie == null) {
throw new IllegalArgumentException("Cookie may not be null");
}
if (origin == null) {
throw new IllegalArgumentException("Cookie origin may not be null");
}
if (cookie instanceof Cookie2) {
Cookie2 cookie2 = (Cookie2) cookie;
int port = origin.getPort();
if (cookie2.isPortAttributeSpecified()) {
if (!portMatch(port, cookie2.getPorts())) {
throw new MalformedCookieException(
"Port attribute violates RFC 2965: "
+ "Request port not found in cookie's port list.");
}
}
}
}
示例8: match
import org.apache.commons.httpclient.Cookie; //導入依賴的package包/類
/**
* Return an array of {@link Cookie}s that should be submitted with a
* request with given attributes, <tt>false</tt> otherwise.
* @param host the host to which the request is being submitted
* @param port the port to which the request is being submitted (currently
* ignored)
* @param path the path to which the request is being submitted
* @param secure <tt>true</tt> if the request is using a secure protocol
* @param cookies an array of <tt>Cookie</tt>s to be matched
* @return an array of <tt>Cookie</tt>s matching the criterium
*/
public Cookie[] match(String host, int port, String path,
boolean secure, final Cookie cookies[]) {
LOG.trace("enter CookieSpecBase.match("
+ "String, int, String, boolean, Cookie[])");
if (cookies == null) {
return null;
}
List matching = new LinkedList();
for (int i = 0; i < cookies.length; i++) {
if (match(host, port, path, secure, cookies[i])) {
addInPathOrder(matching, cookies[i]);
}
}
return (Cookie[]) matching.toArray(new Cookie[matching.size()]);
}
示例9: formatCookies
import org.apache.commons.httpclient.Cookie; //導入依賴的package包/類
/**
* Create a <tt>"Cookie"</tt> header value containing all {@link Cookie}s in
* <i>cookies</i> suitable for sending in a <tt>"Cookie"</tt> header
* @param cookies an array of {@link Cookie}s to be formatted
* @return a string suitable for sending in a Cookie header.
* @throws IllegalArgumentException if an input parameter is illegal
*/
public String formatCookies(Cookie[] cookies)
throws IllegalArgumentException {
LOG.trace("enter CookieSpecBase.formatCookies(Cookie[])");
if (cookies == null) {
throw new IllegalArgumentException("Cookie array may not be null");
}
if (cookies.length == 0) {
throw new IllegalArgumentException("Cookie array may not be empty");
}
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < cookies.length; i++) {
if (i > 0) {
buffer.append("; ");
}
buffer.append(formatCookie(cookies[i]));
}
return buffer.toString();
}
示例10: formatCookieAsVer
import org.apache.commons.httpclient.Cookie; //導入依賴的package包/類
/**
* Return a string suitable for sending in a <tt>"Cookie"</tt> header
* as defined in RFC 2109 for backward compatibility with cookie version 0
* @param buffer The string buffer to use for output
* @param cookie The {@link Cookie} to be formatted as string
* @param version The version to use.
*/
private void formatCookieAsVer(final StringBuffer buffer, final Cookie cookie, int version) {
String value = cookie.getValue();
if (value == null) {
value = "";
}
formatParam(buffer, new NameValuePair(cookie.getName(), value), version);
if ((cookie.getPath() != null) && cookie.isPathAttributeSpecified()) {
buffer.append("; ");
formatParam(buffer, new NameValuePair("$Path", cookie.getPath()), version);
}
if ((cookie.getDomain() != null)
&& cookie.isDomainAttributeSpecified()) {
buffer.append("; ");
formatParam(buffer, new NameValuePair("$Domain", cookie.getDomain()), version);
}
}
示例11: testRFC2965CookiesFormatting
import org.apache.commons.httpclient.Cookie; //導入依賴的package包/類
/**
* Tests RFC 2965 compliant cookies formatting.
*/
public void testRFC2965CookiesFormatting() throws Exception {
CookieSpec cookiespec = new RFC2965Spec();
Cookie2 cookie1 = new Cookie2(".domain.com", "name1",
"value1", "/", null, false, new int[] {80,8080});
cookie1.setVersion(1);
// domain, path, port specified
cookie1.setDomainAttributeSpecified(true);
cookie1.setPathAttributeSpecified(true);
cookie1.setPortAttributeSpecified(true);
Cookie2 cookie2 = new Cookie2(".domain.com", "name2",
null, "/", null, false, null);
cookie2.setVersion(1);
// value null, domain, path, port specified
cookie2.setDomainAttributeSpecified(true);
cookie2.setPathAttributeSpecified(true);
cookie2.setPortAttributeSpecified(false);
Cookie[] cookies = new Cookie[] {cookie1, cookie2};
assertEquals("$Version=\"1\"; name1=\"value1\"; $Domain=\".domain.com\"; $Path=\"/\"; $Port=\"80,8080\"; " +
"name2=\"\"; $Domain=\".domain.com\"; $Path=\"/\"", cookiespec.formatCookies(cookies));
}
示例12: testParseMultipleSamePaths
import org.apache.commons.httpclient.Cookie; //導入依賴的package包/類
public void testParseMultipleSamePaths() throws Exception {
Header header = new Header("Set-Cookie",
"name1=value1;Version=1;Path=/commons,name1=value2;Version=1;Path=/commons");
CookieSpec cookiespec = new CookieSpecBase();
Cookie[] parsed = cookieParse(cookiespec, ".apache.org", 80, "/commons/httpclient", true, header);
HttpState state = new HttpState();
state.addCookies(parsed);
Cookie[] cookies = state.getCookies();
assertEquals("Found 1 cookies.",1,cookies.length);
assertEquals("Name","name1",cookies[0].getName());
assertEquals("Value","value2",cookies[0].getValue());
}
示例13: sort
import org.apache.commons.httpclient.Cookie; //導入依賴的package包/類
private Cookie[] sort(Cookie[] cookies)
{
Cookie[] result = new Cookie[cookies.length];
System.arraycopy(cookies, 0, result, 0, cookies.length);
Arrays.sort(result, COOKIE_NAME_COMPARATOR);
return result;
}
示例14: testValidateNullHost
import org.apache.commons.httpclient.Cookie; //導入依賴的package包/類
public void testValidateNullHost() throws Exception {
CookieSpec cookiespec = new CookieSpecBase();
Cookie cookie = new Cookie();
try {
cookiespec.validate(null, 80, "/", false, cookie);
fail("IllegalArgumentException must have been thrown");
} catch (IllegalArgumentException expected) {
}
}
示例15: testMatchedCookiesOrder
import org.apache.commons.httpclient.Cookie; //導入依賴的package包/類
public void testMatchedCookiesOrder() throws Exception {
CookieSpec cookiespec = new CookieSpecBase();
Cookie[] cookies = {
new Cookie("host", "nomatch", "value", "/noway", null, false),
new Cookie("host", "name2", "value", "/foobar/yada", null, false),
new Cookie("host", "name3", "value", "/foobar", null, false),
new Cookie("host", "name1", "value", "/foobar/yada/yada", null, false)};
Cookie[] matched = cookiespec.match("host", 80, "/foobar/yada/yada", false, cookies);
assertNotNull(matched);
assertEquals(3, matched.length);
assertEquals("name1", matched[0].getName());
assertEquals("name2", matched[1].getName());
assertEquals("name3", matched[2].getName());
}