Java 如何使用 Apache HttpClient 4.3 处理 Cookie

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19136736/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 14:25:59  来源:igfitidea点击:

How to handle Cookies with Apache HttpClient 4.3

javaapachecookiescurlapache-httpclient-4.x

提问by ali

I need to implement a series of HTTP requests in Java and decided to use Apaches HttpClient in version 4.3 (the most current one).

我需要在 Java 中实现一系列 HTTP 请求,并决定在 4.3 版(最新版本)中使用 Apaches HttpClient。

The problem is all these requests use a cookie for session management and I seem to be unable to find a way of accessing that cookie and passing it from request to request. My commands in using curl look something like:

问题是所有这些请求都使用 cookie 进行会话管理,我似乎无法找到访问该 cookie 并将其从请求传递到请求的方法。我使用 curl 的命令类似于:

# Login
curl -c cookies -d "UserName=username&Password=password" "https://example.com/Login"

# Upload a file
curl -b cookies -F fileUpload=@IMG_0013.JPG "https://example.com/File"

# Get results of server processing file
curl -b cookies "https://example.com/File/1234/Content"

They work perfectly. However with HttpClient it seems not to work. What I tried was:

他们完美地工作。但是使用 HttpClient 似乎不起作用。我尝试的是:

    URI serverAddress = new URI("https://example.com/");

    URI loginUri = UriBuilder.fromUri(serverAddress).segment("Login").queryParam("UserName", "username")
            .queryParam("Password", "password").build();

    RequestConfig globalConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.BEST_MATCH).build();
    CookieStore cookieStore = new BasicCookieStore();
    HttpClientContext context = HttpClientContext.create();
    context.setCookieStore(cookieStore);

    CloseableHttpClient httpClient = HttpClients.custom().setDefaultRequestConfig(globalConfig).setDefaultCookieStore(cookieStore).build();
    HttpGet httpGet = new HttpGet(loginUri);
    CloseableHttpResponse loginResponse = httpClient.execute(httpGet,context);

    System.out.println(context.getCookieStore().getCookies());

The output of the last line is always an empty list. I think it should contain my Cookie, am I right?

最后一行的输出总是一个空列表。我认为它应该包含我的 Cookie,对吗?

Can someone give me a small example on how to handle the cookie using Apache HttpClient 4.3?

有人能给我一个关于如何使用 Apache HttpClient 4.3 处理 cookie 的小例子吗?

Thanks

谢谢

采纳答案by ok2c

Your code looks OK to me (other than not releasing resources, but I presume exception handling was omitted for brevity). The reason for cookie store being empty may be violation of the actual cookie policy (which is BEST_MATCH in your case) by the target server. So, cookies sent by the server get rejected as invalid. You can find out if that is the case (and other useful contextual details) by turning on context / wire logging as described here

您的代码对我来说看起来不错(除了不释放资源,但我认为为简洁起见省略了异常处理)。cookie 存储为空的原因可能是目标服务器违反了实际的 cookie 策略(在您的情况下为 BEST_MATCH)。因此,服务器发送的 cookie 被视为无效而被拒绝。您可以通过打开上下文/线路日志记录来确定是否是这种情况(以及其他有用的上下文详细信息),如here所述