您需要首先确定抛出了哪种异常。通过查看 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 中的数据是否正确。