1. Overview
1.概述
In this short tutorial, we’ll see how to get cookies from the Apache HttpClient response.
在这个简短的教程中,我们将看到如何从Apache HttpClient的响应中获取cookies。
First, we’ll show how to send a custom cookie with an HttpClient request. Then, we’ll see how to get it from the response.
首先,我们将展示如何用一个HttpClient请求发送一个自定义cookie。然后,我们将看到如何从响应中获得它。
Please note that the code examples presented here are based on HttpClient 4.3.x and above, so they won’t work on older versions of the API.
请注意,这里介绍的代码示例是基于HttpClient 4.3.x及以上版本的,因此它们不会在旧版本的API上工作。
2. Sending Cookies in the Request
2.在请求中发送Cookies
Before we can get our cookie from the response, we’ll need to create it and send it in a request:
在我们能够从响应中获得我们的cookie之前,我们需要创建它并在请求中发送它。
BasicCookieStore cookieStore = new BasicCookieStore();
BasicClientCookie cookie = new BasicClientCookie("custom_cookie", "test_value");
cookie.setDomain("baeldung.com");
cookie.setAttribute(ClientCookie.DOMAIN_ATTR, "true");
cookie.setPath("/");
cookieStore.addCookie(cookie);
HttpClientContext context = HttpClientContext.create();
context.setAttribute(HttpClientContext.COOKIE_STORE, cookieStore);
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
try (CloseableHttpResponse response = httpClient.execute(new HttpGet("http://www.baeldung.com/"), context)) {
//do something with the response
}
}
First, we create a basic cookie store and a basic cookie with the name custom_cookie and value test_value. Then, we create an HttpClientContext instance that will hold the cookie store. Finally, we pass the created context as an argument to the execute() method.
首先,我们创建一个基本的cookie存储和一个基本的cookie,名称为custom_cookie,值为test_value。然后,我们创建一个HttpClientContext实例,它将保存cookie存储。最后,我们将创建的上下文作为参数传递给execute()方法。
3. Accessing Cookies
3.访问Cookies
Now that we’ve sent a custom cookie in a request, let’s see how to read it from the response:
现在我们已经在请求中发送了一个自定义cookie,让我们看看如何从响应中读取它。
HttpClientContext context = HttpClientContext.create();
context.setAttribute(HttpClientContext.COOKIE_STORE, createCustomCookieStore());
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
try (CloseableHttpResponse response = httpClient.execute(new HttpGet(SAMPLE_URL), context)) {
CookieStore cookieStore = context.getCookieStore();
Cookie customCookie = cookieStore.getCookies()
.stream()
.peek(cookie -> log.info("cookie name:{}", cookie.getName()))
.filter(cookie -> "custom_cookie".equals(cookie.getName()))
.findFirst()
.orElseThrow(IllegalStateException::new);
assertEquals("test_value", customCookie.getValue());
}
}
To get our custom cookie from the response, we must first get the cookie store from the context. Then, we use the getCookies method to get the cookies list. We can then make use of Java streams to iterate over it and search for our cookie. Additionally, we log all cookie names from the store:
为了从响应中获得我们的自定义cookie,我们必须首先从上下文中获得cookie存储。然后,我们使用getCookies方法来获取cookie列表。然后,我们可以利用Java streams来遍历它,并搜索我们的cookie。此外,我们还从商店中记录所有的cookie名称。
[main] INFO c.b.h.c.HttpClientGettingCookieValueTest - cookie name:__cfduid
[main] INFO c.b.h.c.HttpClientGettingCookieValueTest - cookie name:custom_cookie
4. Conclusion
4.总结
In this article, we learned how to get cookies from the Apache HttpClient response.
在这篇文章中,我们学习了如何从Apache HttpClient响应中获取cookie。
As always, the code is available over on GitHub.
像往常一样,代码可在GitHub上获得。