okhttp3.Cookie.matches()方法的使用及代码示例

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

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

Cookie.matches介绍

[英]Returns true if this cookie should be included on a request to url. In addition to this check callers should also confirm that this cookie has not expired.
[中]如果此cookie应包含在url请求中,则返回true。除此之外,呼叫者还应确认此cookie尚未过期。

代码示例

代码示例来源:origin: seven332/EhViewer

/**
 * Get cookies for the url. Fill {@code accepted} and {@code expired}.
 */
public void get(HttpUrl url, List<Cookie> accepted, List<Cookie> expired) {
 long now = System.currentTimeMillis();
 Iterator<Map.Entry<Key, Cookie>> iterator = map.entrySet().iterator();
 while (iterator.hasNext()) {
  Cookie cookie = iterator.next().getValue();
  if (cookie.expiresAt() <= now) {
   iterator.remove();
   expired.add(cookie);
  } else if (cookie.matches(url)) {
   accepted.add(cookie);
  }
 }
}

代码示例来源:origin: GitLqr/LQRWeChat

@Override
synchronized public List<Cookie> loadForRequest(HttpUrl url) {
  List<Cookie> cookiesToRemove = new ArrayList<>();
  List<Cookie> validCookies = new ArrayList<>();
  for (Iterator<Cookie> it = cache.iterator(); it.hasNext(); ) {
    Cookie currentCookie = it.next();
    if (isCookieExpired(currentCookie)) {
      cookiesToRemove.add(currentCookie);
      it.remove();
    } else if (currentCookie.matches(url)) {
      validCookies.add(currentCookie);
    }
  }
  persistor.removeAll(cookiesToRemove);
  return validCookies;
}

代码示例来源:origin: franmontiel/PersistentCookieJar

@Override
synchronized public List<Cookie> loadForRequest(HttpUrl url) {
  List<Cookie> cookiesToRemove = new ArrayList<>();
  List<Cookie> validCookies = new ArrayList<>();
  for (Iterator<Cookie> it = cache.iterator(); it.hasNext(); ) {
    Cookie currentCookie = it.next();
    if (isCookieExpired(currentCookie)) {
      cookiesToRemove.add(currentCookie);
      it.remove();
    } else if (currentCookie.matches(url)) {
      validCookies.add(currentCookie);
    }
  }
  persistor.removeAll(cookiesToRemove);
  return validCookies;
}

代码示例来源:origin: Jamling/SmartIM

@Override
public List<Cookie> loadForRequest(HttpUrl url) {
  List<Cookie> matched = new ArrayList<Cookie>();
  if (this.cookies != null) {
    for (Cookie c : this.cookies) {
      if (c.matches(url)) {
        matched.add(c);
      }
    }
  }
  // Collections.sort(matched, sorter);
  return matched;
}

代码示例来源:origin: Jamling/SmartIM

@Override
public List<Cookie> loadForRequest(HttpUrl url) {
  List<Cookie> matched = new ArrayList<Cookie>();
  if (this.cookies != null) {
    for (Cookie c : this.cookies) {
      if (c.matches(url)) {
        matched.add(c);
      }
    }
  }
  Collections.sort(matched, sorter);
  return matched;
}

代码示例来源:origin: com.couchbase.lite/couchbase-lite-java-core

/**
 * @return return all cookies if httpUrl is null, otherwise return matched cookies.
 */
@Override
public List<okhttp3.Cookie> loadForRequest(HttpUrl httpUrl) {
  List<Cookie> list = new ArrayList<Cookie>();
  if (httpUrl == null) {
    list.addAll(cookies.values());
  } else {
    for (Cookie cookie : cookies.values()) {
      if (cookie.matches(httpUrl))
        list.add(cookie);
    }
  }
  return list;
}

代码示例来源:origin: couchbase/couchbase-lite-java-core

/**
 * @return return all cookies if httpUrl is null, otherwise return matched cookies.
 */
@Override
public List<okhttp3.Cookie> loadForRequest(HttpUrl httpUrl) {
  List<Cookie> list = new ArrayList<Cookie>();
  if (httpUrl == null) {
    list.addAll(cookies.values());
  } else {
    for (Cookie cookie : cookies.values()) {
      if (cookie.matches(httpUrl))
        list.add(cookie);
    }
  }
  return list;
}

代码示例来源:origin: couchbase/couchbase-lite-java-core

static private boolean isMatch(Cookie cookie, URL url) {
  return cookie.matches(HttpUrl.get(url));
}

代码示例来源:origin: com.couchbase.lite/couchbase-lite-java-core

static private boolean isMatch(Cookie cookie, URL url) {
  return cookie.matches(HttpUrl.get(url));
}

代码示例来源:origin: postaddictme/instagram-java-scraper

@Override
public synchronized List<Cookie> loadForRequest(HttpUrl url) {
  List<Cookie> cookies = new ArrayList<>();
  for (Iterator<Cookie> it = cache.iterator(); it.hasNext(); ) {
    Cookie cookie = it.next();
    if (cookie.expiresAt() < System.currentTimeMillis()) {
      it.remove();
    } else if (cookie.matches(url)) {
      cookies.add(cookie);
    }
  }
  return cookies;
}

代码示例来源:origin: postaddictme/instagram-java-scraper

@Override
public synchronized List<Cookie> loadForRequest(HttpUrl url) {
  List<Cookie> cookies = new ArrayList<>();
  for (Iterator<Cookie> it = cache.iterator(); it.hasNext(); ) {
    Cookie cookie = it.next();
    if (cookie.expiresAt() < System.currentTimeMillis()) {
      it.remove();
    } else if (cookie.matches(url)) {
      cookies.add(cookie);
    }
  }
  return cookies;
}

代码示例来源:origin: lfz757077613/MyBlog

@Override
  public List<Cookie> loadForRequest(HttpUrl url) {
    try {
      ArrayList<Cookie> cookies = Lists.newArrayList();
      ConcurrentHashMap<String, Cookie> cookieMap = cookieCache.get(username);
      for (Cookie cookie : cookieMap.values()) {
        if (cookie.matches(url) && cookie.expiresAt() != Long.MIN_VALUE) {
          cookies.add(cookie);
        }
      }
      return cookies;
    } catch (ExecutionException e) {
      log.error("loadForRequest error", e);
      return Collections.emptyList();
    }
  }
}).build();

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

private List<Cookie> loadValidCookies(HttpUrl url) {
  List<Cookie> validCookies = new ArrayList<>();
  List<Cookie> cooky = cookieJar.get(url.host());
  if (cooky != null) {
    Iterator<Cookie> it = cooky.iterator();
    while (it.hasNext()) {
      Cookie currentCookie = it.next();
      if (isCookieExpired(currentCookie)) {
        it.remove();
      } else if (currentCookie.matches(url)) {
        validCookies.add(currentCookie);
      }
    }
  }
  return validCookies;
}

代码示例来源:origin: FussenYu/MVP_Project

@Override
synchronized public List<Cookie> loadForRequest(HttpUrl url) {
  List<Cookie> cookiesToRemove = new ArrayList<>();
  List<Cookie> validCookies = new ArrayList<>();
  for (Iterator<Cookie> it = cache.iterator(); it.hasNext(); ) {
    Cookie currentCookie = it.next();
    if (isCookieExpired(currentCookie)) {
      cookiesToRemove.add(currentCookie);
      it.remove();
    } else if (currentCookie.matches(url)) {
      validCookies.add(currentCookie);
    }
  }
  persistor.removeAll(cookiesToRemove);
  return validCookies;
}

代码示例来源:origin: com.couchbase.lite/couchbase-lite-java-core

@Override
@InterfaceAudience.Private
synchronized public void deleteCookie(URL url) {
  // since CookieStore does not have a way to delete an individual cookie, do workaround:
  // 1. get all cookies
  // 2. filter list to strip out the one we want to delete
  // 3. clear cookie store
  // 4. re-add all cookies except the one we want to delete
  if (cookieJar == null)
    return;
  List<Cookie> cookies = cookieJar.loadForRequest(null);
  List<Cookie> retainedCookies = new ArrayList<Cookie>();
  for (Cookie cookie : cookies) {
    // matching rely on OkHttp's matching logic
    // https://square.github.io/okhttp/3.x/okhttp/okhttp3/Cookie.html#matches-okhttp3.HttpUrl-
    if (!cookie.matches(HttpUrl.get(url)))
      retainedCookies.add(cookie);
  }
  cookieJar.clear();
  // TODO: HttpUrl parameter should be revisited.
  cookieJar.saveFromResponse(null, retainedCookies);
}

代码示例来源:origin: couchbase/couchbase-lite-java-core

@Override
@InterfaceAudience.Private
synchronized public void deleteCookie(URL url) {
  // since CookieStore does not have a way to delete an individual cookie, do workaround:
  // 1. get all cookies
  // 2. filter list to strip out the one we want to delete
  // 3. clear cookie store
  // 4. re-add all cookies except the one we want to delete
  if (cookieJar == null)
    return;
  List<Cookie> cookies = cookieJar.loadForRequest(null);
  List<Cookie> retainedCookies = new ArrayList<Cookie>();
  for (Cookie cookie : cookies) {
    // matching rely on OkHttp's matching logic
    // https://square.github.io/okhttp/3.x/okhttp/okhttp3/Cookie.html#matches-okhttp3.HttpUrl-
    if (!cookie.matches(HttpUrl.get(url)))
      retainedCookies.add(cookie);
  }
  cookieJar.clear();
  // TODO: HttpUrl parameter should be revisited.
  cookieJar.saveFromResponse(null, retainedCookies);
}

相关文章