关于Cookie的问题

时间:2018-07-09 02:22:51

标签: cookies

以下有关Cookie的问题需要帮助

  1. 我正在开发一个独立的jar,可以访问代码中的http网站。当我访问http站点时,cookie将存储在客户端计算机中?
  2. 何时将cookie保存在客户端计算机中?
  3. 仅当我们通过浏览器或任何库访问Web应用程序并执行http请求时,才会存储cookie
  4. 何时以及如何在客户端计算机上启用或禁用cookie。

谢谢。

1 个答案:

答案 0 :(得分:1)

您的Java应用程序将需要创建一个cookie存储并将其链接到HttpClient。例如

CookieStore cookieStore = new BasicCookieStore();
// Populate cookies if needed
BasicClientCookie cookie = new BasicClientCookie("name", "value");
cookie.setDomain(".mycompany.com");
cookie.setPath("/");
cookieStore.addCookie(cookie);
// Set the store
CloseableHttpClient httpclient = HttpClients.custom()
        .setDefaultCookieStore(cookieStore)
        .build();

参考:https://hc.apache.org/httpcomponents-client-ga/tutorial/html/statemgmt.html