org.apache.http.cookie.Cookie类的使用及代码示例

x33g5p2x  于2022-01-18 转载在 其他  
字(10.0k)|赞(0)|评价(0)|浏览(287)

本文整理了Java中org.apache.http.cookie.Cookie类的一些代码示例,展示了Cookie类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Cookie类的具体详情如下:
包路径:org.apache.http.cookie.Cookie
类名称:Cookie

Cookie介绍

[英]HTTP "magic-cookie" represents a piece of state information that the HTTP agent and the target server can exchange to maintain a session.
[中]HTTP“magic cookie”表示HTTP代理和目标服务器可以交换以维护会话的一段状态信息。

代码示例

代码示例来源:origin: internetarchive/heritrix3

line.append(cookie.getDomain());
line.append(tab);
line.append(cookie.getPath() != null ? cookie.getPath() : "/");
line.append(tab);
line.append(cookie.isSecure() ? "TRUE" : "FALSE");
line.append(tab);
line.append(cookie.getExpiryDate() != null ? cookie.getExpiryDate().getTime() / 1000 : -1);
line.append(tab);
line.append(cookie.getName());
line.append(tab);
line.append(cookie.getValue() != null ? cookie.getValue() : "");
line.append("\n");
out.write(line.toString().getBytes());

代码示例来源:origin: robovm/robovm

private List<Header> doFormatManyHeaders(final List<Cookie> cookies) {
  List<Header> headers = new ArrayList<Header>(cookies.size());
  for (Cookie cookie : cookies) {
    int version = cookie.getVersion();
    CharArrayBuffer buffer = new CharArrayBuffer(40);
    buffer.append("Cookie: ");
    buffer.append("$Version=");
    buffer.append(Integer.toString(version));
    buffer.append("; ");
    formatCookieAsVer(buffer, cookie, version);
    headers.add(new BufferedHeader(buffer));
  }
  return headers;
}

代码示例来源:origin: robovm/robovm

/**
 * Removes all of {@link Cookie cookies} in this HTTP state
 * that have expired by the specified {@link java.util.Date date}. 
 * 
 * @return true if any cookies were purged.
 * 
 * @see Cookie#isExpired(Date)
 */
public synchronized boolean clearExpired(final Date date) {
  if (date == null) {
    return false;
  }
  boolean removed = false;
  for (Iterator<Cookie> it = cookies.iterator(); it.hasNext();) {
    if (it.next().isExpired(date)) {
      it.remove();
      removed = true;
    }
  }
  return removed;
}

代码示例来源:origin: robovm/robovm

/**
 * Don't log the cookie's value; that's potentially sensitive information.
 */
private String cookieToString(Cookie cookie) {
  return cookie.getClass().getSimpleName()
      + "[version=" + cookie.getVersion()
      + ",name=" + cookie.getName()
      + ",domain=" + cookie.getDomain()
      + ",path=" + cookie.getPath()
      + ",expiry=" + cookie.getExpiryDate()
      + "]";
}
// END android-added

代码示例来源:origin: robovm/robovm

public int compare(final Cookie c1, final Cookie c2) {
  int res = c1.getName().compareTo(c2.getName());
  if (res == 0) {
    // do not differentiate empty and null domains 
    String d1 = c1.getDomain();
    if (d1 == null) {
      d1 = "";
    }
    String d2 = c2.getDomain();
    if (d2 == null) {
      d2 = "";
    }
    res = d1.compareToIgnoreCase(d2);
  }
  return res;
}

代码示例来源:origin: internetarchive/heritrix3

public void addCookie(Cookie cookie) {
  if (isCookieCountMaxedForDomain(cookie.getDomain())) {
    logger.log(
        Level.FINEST,
        "Maximum number of cookies reached for domain "
            + cookie.getDomain() + ". Will not add new cookie "
            + cookie.getName() + " with value "
            + cookie.getValue());
    return;
  }
  addCookieImpl(cookie);
}

代码示例来源:origin: grzegorznittner/chanu

private void writeObject(ObjectOutputStream out) throws IOException {
  out.writeObject(cookie.getName());
  out.writeObject(cookie.getValue());
  out.writeObject(cookie.getComment());
  out.writeObject(cookie.getDomain());
  out.writeObject(cookie.getExpiryDate());
  out.writeObject(cookie.getPath());
  out.writeInt(cookie.getVersion());
  out.writeBoolean(cookie.isSecure());
}

代码示例来源:origin: robovm/robovm

/**
 * 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 char array buffer to use for output
 * @param cookie The {@link Cookie} to be formatted as string
 * @param version The version to use.
 */
protected void formatCookieAsVer(final CharArrayBuffer buffer, 
    final Cookie cookie, int version) {
  formatParamAsVer(buffer, cookie.getName(), cookie.getValue(), version);
  if (cookie.getPath() != null) {
    if (cookie instanceof ClientCookie 
        && ((ClientCookie) cookie).containsAttribute(ClientCookie.PATH_ATTR)) {
      buffer.append("; ");
      formatParamAsVer(buffer, "$Path", cookie.getPath(), version);
    }
  }
  if (cookie.getDomain() != null) {
    if (cookie instanceof ClientCookie 
        && ((ClientCookie) cookie).containsAttribute(ClientCookie.DOMAIN_ATTR)) {
      buffer.append("; ");
      formatParamAsVer(buffer, "$Domain", cookie.getDomain(), version);
    }
  }
}

代码示例来源:origin: robovm/robovm

public List<Header> formatCookies(final List<Cookie> cookies) {
  if (cookies == null) {
    throw new IllegalArgumentException("List of cookies may not be null");
  }
  if (cookies.isEmpty()) {
    throw new IllegalArgumentException("List of cookies may not be empty");
  }
  CharArrayBuffer buffer = new CharArrayBuffer(20 * cookies.size());
  buffer.append(SM.COOKIE);
  buffer.append(": ");
  for (int i = 0; i < cookies.size(); i++) {
    Cookie cookie = cookies.get(i);
    if (i > 0) {
      buffer.append("; ");
    }
    buffer.append(cookie.getName());
    String s = cookie.getValue();
    if (s != null) {
      buffer.append("=");
      buffer.append(s);
    }
  }
  List<Header> headers = new ArrayList<Header>(1);
  headers.add(new BufferedHeader(buffer));
  return headers;
}

代码示例来源:origin: yaphone/itchat4j

public static String getCookie(String name) {
  List<Cookie> cookies = cookieStore.getCookies();
  for (Cookie cookie : cookies) {
    if (cookie.getName().equalsIgnoreCase(name)) {
      return cookie.getValue();
    }
  }
  return null;
}

代码示例来源:origin: ibinti/bugvm

@Override
public boolean match(final Cookie cookie, final CookieOrigin origin) {
  Args.notNull(cookie, "Cookie");
  Args.notNull(origin, "Cookie origin");
  final String host = origin.getHost();
  final String domain = cookie.getDomain();
  if (domain == null) {
    return false;
  }
  return host.endsWith(domain);
}

代码示例来源:origin: ibinti/bugvm

@Override
public boolean match(final Cookie cookie, final CookieOrigin origin) {
  Args.notNull(cookie, "Cookie");
  Args.notNull(origin, "Cookie origin");
  return !cookie.isSecure() || origin.isSecure();
}

代码示例来源:origin: org.leapframework/leap-webunit

public synchronized void addCookie(final org.apache.http.cookie.Cookie cookie) {
  if (cookie != null) {
    log.debug("Add cookie : name={}, path={}, value={},", cookie.getName(), cookie.getPath(), cookie.getValue());
    // first remove any old cookie that is equivalent
    cookies.remove(cookie);
    if (!cookie.isExpired(new Date())) {
      cookies.add(cookie);
    }
  }
}

代码示例来源:origin: internetarchive/heritrix3

/**
 * Returns a string that uniquely identifies the cookie, The format The
 * format of the key is {@code "normalizedDomain;name;path"}. Adapted from
 * {@link CookieIdentityComparator#compare(Cookie, Cookie)}.
 */
protected String sortableKey(Cookie cookie) {
  String normalizedDomain = normalizeHost(cookie.getDomain());
  // use ";" as delimiter since it is the delimiter in the cookie header,
  // so presumably can't appear in any of these values
  StringBuilder buf = new StringBuilder(normalizedDomain);
  buf.append(";").append(cookie.getName());
  buf.append(";").append(cookie.getPath() != null ? cookie.getPath() : "/");
  return buf.toString();
}

代码示例来源:origin: ibinti/bugvm

@Override
public boolean match(final Cookie cookie, final CookieOrigin origin) {
  Args.notNull(cookie, "Cookie");
  Args.notNull(origin, "Cookie origin");
  return pathMatch(origin.getPath(), cookie.getPath());
}

代码示例来源:origin: ViDA-NYU/ache

/**
 * Adds an {@link Cookie HTTP cookie}, replacing any existing equivalent cookies. If the given
 * cookie has already expired it will not be added, but existing values will still be removed.
 *
 * @param cookie the {@link Cookie cookie} to be added
 *
 * @see #addCookies(HashMap)
 *
 */
@Override
public void addCookie(final Cookie cookie) {
  if (cookie != null) {
    // first remove any old cookie that is equivalent
    cookies.remove(cookie.getDomain() + cookie.getName() + cookie.getPath());
    if (!cookie.isExpired(new Date())) {
      cookies.put(cookie.getDomain() + cookie.getName() + cookie.getPath(), cookie);
    }
  }
}

代码示例来源:origin: robovm/robovm

@Override
public boolean match(Cookie cookie, 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 host = origin.getHost();
    String domain = cookie.getDomain();
    if (domain == null) {
      return false;
    }
    return host.endsWith(domain);
  }

代码示例来源:origin: yiwent/Mobike

@Override
public void addCookie(Cookie cookie) {
  if (omitNonPersistentCookies && !cookie.isPersistent())
    return;
  String name = cookie.getName() + cookie.getDomain();
  // Save cookie into local store, or remove if expired
  if (!cookie.isExpired(new Date())) {
    cookies.put(name, cookie);
  } else {
    cookies.remove(name);
  }
  // Save cookie into persistent store
  SharedPreferences.Editor prefsWriter = cookiePrefs.edit();
  prefsWriter.putString(COOKIE_NAME_STORE, TextUtils.join(",", cookies.keySet()));
  prefsWriter.putString(COOKIE_NAME_PREFIX + name, encodeCookie(new SerializableCookie(cookie)));
  prefsWriter.commit();
}

代码示例来源:origin: grzegorznittner/chanu

@Override
public void addCookie(Cookie cookie) {
  String name = cookie.getName();
  // Save cookie into local store, or remove if expired
  if(!cookie.isExpired(new Date())) {
    cookies.put(name, cookie);
  } else {
    cookies.remove(name);
  }
  // Save cookie into persistent store
  SharedPreferences.Editor prefsWriter = cookiePrefs.edit();
  prefsWriter.putString(COOKIE_NAME_STORE, TextUtils.join(",", cookies.keySet()));
  prefsWriter.putString(COOKIE_NAME_PREFIX + name, encodeCookie(new SerializableCookie(cookie)));
  prefsWriter.commit();
}

代码示例来源:origin: apache/hive

/**
 * The function iterates through the list of cookies in the cookiestore and tries to
 * match them with the cookieName. If there is a match, the cookieStore already
 * has a valid cookie and the client need not send Credentials for validation purpose.
 * @param cookieStore The cookie Store
 * @param cookieName Name of the cookie which needs to be validated
 * @param isSSL Whether this is a http/https connection
 * @return true or false based on whether the client needs to send the credentials or
 * not to the server.
 */
static boolean needToSendCredentials(CookieStore cookieStore, String cookieName, boolean isSSL) {
 if (cookieName == null || cookieStore == null) {
  return true;
 }
 List<Cookie> cookies = cookieStore.getCookies();
 for (Cookie c : cookies) {
  // If this is a secured cookie and the current connection is non-secured,
  // then, skip this cookie. We need to skip this cookie because, the cookie
  // replay will not be transmitted to the server.
  if (c.isSecure() && !isSSL) {
   continue;
  }
  if (c.getName().equals(cookieName)) {
   return false;
  }
 }
 return true;
}

相关文章