com.gargoylesoftware.htmlunit.WebClient.getCookies()方法的使用及代码示例

x33g5p2x  于2022-02-02 转载在 其他  
字(8.4k)|赞(0)|评价(0)|浏览(181)

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

WebClient.getCookies介绍

[英]Returns the currently configured cookies applicable to the specified URL, in an unmodifiable set. If disabled, this returns an empty set.
[中]以不可修改的集合返回适用于指定URL的当前配置cookie。如果禁用,则返回一个空集。

代码示例

代码示例来源:origin: spring-projects/spring-framework

private void cookies(MockHttpServletRequest request) {
  List<Cookie> cookies = new ArrayList<>();
  String cookieHeaderValue = header("Cookie");
  if (cookieHeaderValue != null) {
    StringTokenizer tokens = new StringTokenizer(cookieHeaderValue, "=;");
    while (tokens.hasMoreTokens()) {
      String cookieName = tokens.nextToken().trim();
      Assert.isTrue(tokens.hasMoreTokens(),
          () -> "Expected value for cookie name '" + cookieName +
              "': full cookie header was [" + cookieHeaderValue + "]");
      String cookieValue = tokens.nextToken().trim();
      processCookie(request, cookies, new Cookie(cookieName, cookieValue));
    }
  }
  Set<com.gargoylesoftware.htmlunit.util.Cookie> managedCookies = this.webClient.getCookies(this.webRequest.getUrl());
  for (com.gargoylesoftware.htmlunit.util.Cookie cookie : managedCookies) {
    processCookie(request, cookies, new Cookie(cookie.getName(), cookie.getValue()));
  }
  Cookie[] parentCookies = request.getCookies();
  if (parentCookies != null) {
    for (Cookie cookie : parentCookies) {
      cookies.add(cookie);
    }
  }
  if (!ObjectUtils.isEmpty(cookies)) {
    request.setCookies(cookies.toArray(new Cookie[0]));
  }
}

代码示例来源:origin: spring-projects/spring-security

private void cookies(WebTestClient.RequestBodySpec request, WebRequest webRequest) {
  String cookieHeaderValue = header("Cookie", webRequest);
  if (cookieHeaderValue != null) {
    StringTokenizer tokens = new StringTokenizer(cookieHeaderValue, "=;");
    while (tokens.hasMoreTokens()) {
      String cookieName = tokens.nextToken().trim();
      Assert.isTrue(tokens.hasMoreTokens(),
          () -> "Expected value for cookie name '" + cookieName +
              "': full cookie header was [" + cookieHeaderValue + "]");
      String cookieValue = tokens.nextToken().trim();
      request.cookie(cookieName, cookieValue);
    }
  }
  Set<com.gargoylesoftware.htmlunit.util.Cookie> managedCookies = this.webClient.getCookies(webRequest.getUrl());
  for (com.gargoylesoftware.htmlunit.util.Cookie cookie : managedCookies) {
    request.cookie(cookie.getName(), cookie.getValue());
  }
}

代码示例来源:origin: org.seleniumhq.selenium/selenium-htmlunit-driver

@Override
public Set<Cookie> getCookies() {
 URL url = getRawUrl();
 // The about:blank URL (the default in case no navigation took place)
 // does not have a valid 'hostname' part and cannot be used for creating
 // cookies based on it - return an empty set.
 if (!url.toString().startsWith("http")) {
  return Sets.newHashSet();
 }
 return ImmutableSet.copyOf(Collections2.transform(
   getWebClient().getCookies(url),
   htmlUnitCookieToSeleniumCookieTransformer));
}

代码示例来源:origin: net.sourceforge.htmlunit/htmlunit

/**
 * {@inheritDoc}
 */
@Override
public List<HttpCookie> get(final URI uri) {
  final List<HttpCookie> cookies = new ArrayList<>();
  try {
    final String urlString = uri.toString().replace("ws://", "http://").replace("wss://", "https://");
    final java.net.URL url = new java.net.URL(urlString);
    for (final Cookie cookie : webClient_.getCookies(url)) {
      final HttpCookie httpCookie = new HttpCookie(cookie.getName(), cookie.getValue());
      httpCookie.setVersion(0);
      cookies.add(httpCookie);
    }
  }
  catch (final Exception e) {
    throw new RuntimeException(e);
  }
  return cookies;
}

代码示例来源:origin: HtmlUnit/htmlunit

/**
 * {@inheritDoc}
 */
@Override
public List<HttpCookie> get(final URI uri) {
  final List<HttpCookie> cookies = new ArrayList<>();
  try {
    final String urlString = uri.toString().replace("ws://", "http://").replace("wss://", "https://");
    final java.net.URL url = new java.net.URL(urlString);
    for (final Cookie cookie : webClient_.getCookies(url)) {
      final HttpCookie httpCookie = new HttpCookie(cookie.getName(), cookie.getValue());
      httpCookie.setVersion(0);
      cookies.add(httpCookie);
    }
  }
  catch (final Exception e) {
    throw new RuntimeException(e);
  }
  return cookies;
}

代码示例来源:origin: org.apache.servicemix.bundles/org.apache.servicemix.bundles.spring-test

private void cookies(MockHttpServletRequest request) {
  List<Cookie> cookies = new ArrayList<>();
  String cookieHeaderValue = header("Cookie");
  if (cookieHeaderValue != null) {
    StringTokenizer tokens = new StringTokenizer(cookieHeaderValue, "=;");
    while (tokens.hasMoreTokens()) {
      String cookieName = tokens.nextToken().trim();
      Assert.isTrue(tokens.hasMoreTokens(),
          () -> "Expected value for cookie name '" + cookieName +
              "': full cookie header was [" + cookieHeaderValue + "]");
      String cookieValue = tokens.nextToken().trim();
      processCookie(request, cookies, new Cookie(cookieName, cookieValue));
    }
  }
  Set<com.gargoylesoftware.htmlunit.util.Cookie> managedCookies = this.webClient.getCookies(this.webRequest.getUrl());
  for (com.gargoylesoftware.htmlunit.util.Cookie cookie : managedCookies) {
    processCookie(request, cookies, new Cookie(cookie.getName(), cookie.getValue()));
  }
  Cookie[] parentCookies = request.getCookies();
  if (parentCookies != null) {
    for (Cookie cookie : parentCookies) {
      cookies.add(cookie);
    }
  }
  if (!ObjectUtils.isEmpty(cookies)) {
    request.setCookies(cookies.toArray(new Cookie[0]));
  }
}

代码示例来源:origin: apache/servicemix-bundles

private void cookies(MockHttpServletRequest request) {
  List<Cookie> cookies = new ArrayList<Cookie>();
  String cookieHeaderValue = header("Cookie");
  if (cookieHeaderValue != null) {
    StringTokenizer tokens = new StringTokenizer(cookieHeaderValue, "=;");
    while (tokens.hasMoreTokens()) {
      String cookieName = tokens.nextToken().trim();
      if (!tokens.hasMoreTokens()) {
        throw new IllegalArgumentException("Expected value for cookie name '" + cookieName +
            "': full cookie header was [" + cookieHeaderValue + "]");
      }
      String cookieValue = tokens.nextToken().trim();
      processCookie(request, cookies, new Cookie(cookieName, cookieValue));
    }
  }
  Set<com.gargoylesoftware.htmlunit.util.Cookie> managedCookies = this.webClient.getCookies(this.webRequest.getUrl());
  for (com.gargoylesoftware.htmlunit.util.Cookie cookie : managedCookies) {
    processCookie(request, cookies, new Cookie(cookie.getName(), cookie.getValue()));
  }
  Cookie[] parentCookies = request.getCookies();
  if (parentCookies != null) {
    for (Cookie cookie : parentCookies) {
      cookies.add(cookie);
    }
  }
  if (!ObjectUtils.isEmpty(cookies)) {
    request.setCookies(cookies.toArray(new Cookie[cookies.size()]));
  }
}

代码示例来源:origin: net.sourceforge.htmlunit/htmlunit

/**
 * {@inheritDoc}
 */
@Override
@JsxGetter
public String getCookie() {
  final HtmlPage page = getPage();
  final URL url = page.getUrl();
  final StringBuilder builder = new StringBuilder();
  final Set<Cookie> cookies = page.getWebClient().getCookies(url);
  for (final Cookie cookie : cookies) {
    if (cookie.isHttpOnly()) {
      continue;
    }
    if (builder.length() != 0) {
      builder.append("; ");
    }
    if (!HtmlUnitBrowserCompatCookieSpec.EMPTY_COOKIE_NAME.equals(cookie.getName())) {
      builder.append(cookie.getName());
      builder.append("=");
    }
    builder.append(cookie.getValue());
  }
  return builder.toString();
}

代码示例来源:origin: org.seleniumhq.selenium/selenium-htmlunit-driver

@Override
public void deleteCookieNamed(String name) {
 CookieManager cookieManager = getWebClient().getCookieManager();
 URL url = getRawUrl();
 Set<com.gargoylesoftware.htmlunit.util.Cookie> rawCookies = getWebClient().getCookies(url);
 for (com.gargoylesoftware.htmlunit.util.Cookie cookie : rawCookies) {
  if (name.equals(cookie.getName())) {
   cookieManager.removeCookie(cookie);
  }
 }
}

代码示例来源:origin: HtmlUnit/htmlunit

/**
 * {@inheritDoc}
 */
@Override
@JsxGetter
public String getCookie() {
  final HtmlPage page = getPage();
  final URL url = page.getUrl();
  final StringBuilder builder = new StringBuilder();
  final Set<Cookie> cookies = page.getWebClient().getCookies(url);
  for (final Cookie cookie : cookies) {
    if (cookie.isHttpOnly()) {
      continue;
    }
    if (builder.length() != 0) {
      builder.append("; ");
    }
    if (!HtmlUnitBrowserCompatCookieSpec.EMPTY_COOKIE_NAME.equals(cookie.getName())) {
      builder.append(cookie.getName());
      builder.append('=');
    }
    builder.append(cookie.getValue());
  }
  return builder.toString();
}

代码示例来源:origin: org.seleniumhq.selenium/selenium-htmlunit-driver

@Override
public void deleteAllCookies() {
 CookieManager cookieManager = getWebClient().getCookieManager();
 URL url = getRawUrl();
 Set<com.gargoylesoftware.htmlunit.util.Cookie> rawCookies = getWebClient().getCookies(url);
 for (com.gargoylesoftware.htmlunit.util.Cookie cookie : rawCookies) {
   cookieManager.removeCookie(cookie);
 }
}

相关文章

WebClient类方法