Apache HttpComponents CookieStore 不存储 Cookie
Apache HttpComponents CookieStore Not Storing Cookies
我正在使用 HttpComponents 4.5.2 并且我正在尝试存储 cookie,因为我需要将它们用于登录和其他请求。当应用程序仍然 运行 时代码工作正常,但这里的问题是当我重新启动它时,应该存储在 CookieStore 中的 cookie 不存在。这是我写的:
public static void main( String[] args ) throws InterruptedException
{
RequestConfig globalConfig = RequestConfig.custom()
.setCookieSpec(CookieSpecs.STANDARD).build();
BasicCookieStore cookieStore = new BasicCookieStore();
HttpClientContext context = HttpClientContext.create();
context.setCookieStore(cookieStore);
CloseableHttpAsyncClient httpclient = HttpAsyncClients.custom()
.setDefaultRequestConfig(globalConfig)
.setDefaultCookieStore(cookieStore)
.build();
httpclient.start();
login(httpclient, context);
}
public static void login(CloseableHttpAsyncClient httpClient, HttpClientContext context) throws InterruptedException
{
JSONObject json = new JSONObject("{ email : blahblahblah1, password : blahblahblah2 }");
StringEntity requestEntity = new StringEntity(
json.toString(),
ContentType.APPLICATION_JSON);
HttpPost postMethod = new HttpPost("http://localhost:8080/login");
postMethod.setEntity(requestEntity);
final CountDownLatch latch = new CountDownLatch(1);
httpClient.execute(postMethod, context, new FutureCallback<HttpResponse>() {
public void completed(final HttpResponse response) {
latch.countDown();
System.out.println(postMethod.getRequestLine() + "->" + response.getStatusLine());
//System.out.println(context.getCookieStore().getCookies().size());
}
public void failed(final Exception ex) {
latch.countDown();
System.out.println(postMethod.getRequestLine() + "->" + ex);
}
public void cancelled() {
latch.countDown();
System.out.println(postMethod.getRequestLine() + " cancelled");
}
});
latch.await();
}
我已阅读 HttpComponents 文档和关于 cookie 的第 3.5 节说:
HttpClient can work with any physical representation of a persistent cookie store that implements the CookieStore interface. The default CookieStore implementation called BasicCookieStore is a simple implementation backed by a java.util.ArrayList. Cookies stored in an BasicClientCookie object are lost when the container object get garbage collected. Users can provide more complex implementations if necessary
所以我想知道是否留给用户来实现某种可以有效存储 cookie 的结构,或者我是否遗漏了什么。
是的,使用 ArrayList
支持的 BasicCookieStore
意味着当您的 jvm 存在时,那里的数据就像内存中的任何 ArrayList 一样丢失。
BasicCookieStore
class 还实现了 Serializable
,因此您可以使用它来将其保存到磁盘并在您的应用启动时恢复(如果该文件存在)。
您可以从验证该流程的测试中借用一些代码 TestBasicCookieStore#testSerialization。
我正在使用 HttpComponents 4.5.2 并且我正在尝试存储 cookie,因为我需要将它们用于登录和其他请求。当应用程序仍然 运行 时代码工作正常,但这里的问题是当我重新启动它时,应该存储在 CookieStore 中的 cookie 不存在。这是我写的:
public static void main( String[] args ) throws InterruptedException
{
RequestConfig globalConfig = RequestConfig.custom()
.setCookieSpec(CookieSpecs.STANDARD).build();
BasicCookieStore cookieStore = new BasicCookieStore();
HttpClientContext context = HttpClientContext.create();
context.setCookieStore(cookieStore);
CloseableHttpAsyncClient httpclient = HttpAsyncClients.custom()
.setDefaultRequestConfig(globalConfig)
.setDefaultCookieStore(cookieStore)
.build();
httpclient.start();
login(httpclient, context);
}
public static void login(CloseableHttpAsyncClient httpClient, HttpClientContext context) throws InterruptedException
{
JSONObject json = new JSONObject("{ email : blahblahblah1, password : blahblahblah2 }");
StringEntity requestEntity = new StringEntity(
json.toString(),
ContentType.APPLICATION_JSON);
HttpPost postMethod = new HttpPost("http://localhost:8080/login");
postMethod.setEntity(requestEntity);
final CountDownLatch latch = new CountDownLatch(1);
httpClient.execute(postMethod, context, new FutureCallback<HttpResponse>() {
public void completed(final HttpResponse response) {
latch.countDown();
System.out.println(postMethod.getRequestLine() + "->" + response.getStatusLine());
//System.out.println(context.getCookieStore().getCookies().size());
}
public void failed(final Exception ex) {
latch.countDown();
System.out.println(postMethod.getRequestLine() + "->" + ex);
}
public void cancelled() {
latch.countDown();
System.out.println(postMethod.getRequestLine() + " cancelled");
}
});
latch.await();
}
我已阅读 HttpComponents 文档和关于 cookie 的第 3.5 节说:
HttpClient can work with any physical representation of a persistent cookie store that implements the CookieStore interface. The default CookieStore implementation called BasicCookieStore is a simple implementation backed by a java.util.ArrayList. Cookies stored in an BasicClientCookie object are lost when the container object get garbage collected. Users can provide more complex implementations if necessary
所以我想知道是否留给用户来实现某种可以有效存储 cookie 的结构,或者我是否遗漏了什么。
是的,使用 ArrayList
支持的 BasicCookieStore
意味着当您的 jvm 存在时,那里的数据就像内存中的任何 ArrayList 一样丢失。
BasicCookieStore
class 还实现了 Serializable
,因此您可以使用它来将其保存到磁盘并在您的应用启动时恢复(如果该文件存在)。
您可以从验证该流程的测试中借用一些代码 TestBasicCookieStore#testSerialization。