【问题标题】:Dealing with Java CookieManager "Invalid cookie" errors处理 Java CookieManager "Invalid cookie" 错误
【发布时间】:2016-12-25 09:55:13
【问题描述】:

我定义了一个CookieStore,如下:

CookieManager cookieManager = new CookieManager();
CookieHandler.setDefault(cookieManager );
cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL);

每当我使用HttpURLConnection 完成请求时:

URL url = new URL(MY_URL);
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();

我在我的输出中得到了这个:

java.net.CookieManager put SEVERE: Invalid cookie for https://...: ; HttpOnly

我应该如何处理这条消息?

【问题讨论】:

    标签: java http cookies


    【解决方案1】:

    您需要首先确定抛出了哪种异常。通过查看 CookieManger 的文档:http://www.docjar.com/html/api/java/net/CookieManager.java.html

      283                       try {
      284                           cookies = HttpCookie.parse(headerValue);
      285                       } catch (IllegalArgumentException e) {
      286                           // Bogus header, make an empty list and log the error
      287                           cookies = java.util.Collections.EMPTY_LIST;
      288                           if (logger.isLoggable(PlatformLogger.SEVERE)) {
      289                               logger.severe("Invalid cookie for " + uri + ": " + headerValue);
      290                           }
      291                       }
    

    问题似乎在于您的请求标头不正确。可能想研究一下,这里是示例代码的链接。

    http://www.programcreek.com/java-api-examples/index.php?api=java.net.CookieManager

    此外,您可能还想使用 chrome 调试器来查看发出的实际请求,通常它会为您提供有关请求失败原因的更多信息。请求可能不正确,您尝试将其发送到的 url 可能无效,您发送请求的服务可能需要某些参数。

    从代码中,它似乎在响应中寻找标头。但是,响应本身要么不包含标头,要么有问题,因此 HttpCookie.parse 会抛出错误。

    如果您查看 HttpCookies.parse,它会在以下情况下引发异常:

    投掷:

    IllegalArgumentException - if header string violates the cookie specification's syntax, or the cookie name contains llegal characters, or the cookie name is one of the tokens reserved for use by the cookie protocol
    NullPointerException - if the header string is null
    

    所以你需要查看响应,看看他们放在 header 中的数据是否正确。

    【讨论】:

      猜你喜欢
      • 2013-04-15
      • 1970-01-01
      • 2013-01-29
      • 1970-01-01
      • 2012-08-22
      • 2017-02-25
      • 1970-01-01
      • 2020-01-02
      • 1970-01-01
      相关资源
      最近更新 更多