Apache HttpClient - Cookies 管理
-
简述
Cookie 是存储在客户端计算机上的文本文件,用于各种信息跟踪目的。HttpClient 提供对 cookie 的支持,您可以创建和管理 cookie。 -
创建cookie
按照下面给出的步骤使用 HttpClient 库创建一个 cookie。第 1 步 - 创建 Cookiestore 对象
CookieStore 接口代表 Cookie 对象的抽象存储。您可以通过实例化 BasicCookieStore 类来创建 cookie 存储,这是该接口的默认实现。//创建CookieStore对象 CookieStore cookieStore = new BasicCookieStore();
第 2 步 - 创建 ClientCookie 对象
除了 cookie 的功能,ClientCookie 还可以获取服务器中的原始 cookie。您可以通过实例化 BasicClientCookie 类来创建客户端 cookie。对于此类的构造函数,您需要传递您希望存储在该特定 cookie 中的键值对。//创建客户端cookie BasicClientCookie clientCookie = new BasicClientCookie("name","Raju");
第 3 步 - 为 cookie 设置值
对于客户端cookie,您可以使用相应的方法设置/删除路径、值、版本、到期日期、域、注释和属性。Calendar myCal = new GregorianCalendar(2018, 9, 26); Date expiryDate = myCal.getTime(); clientcookie.setExpiryDate(expiryDate); clientcookie.setPath("/"); clientcookie.setSecure(true); clientcookie.setValue("25"); clientcookie.setVersion(5);
第 4 步 - 将 cookie 添加到 cookie 存储中
您可以使用 BasicCookieStore 类的 addCookie() 方法将 cookie 添加到 cookie 存储中。将所需的 cookie 添加到 Cookiestore。//将创建的cookies添加到cookie store cookiestore.addCookie(clientcookie);
-
示例
以下示例演示了如何创建 cookie 并将它们添加到 cookie 存储中。在这里,我们创建了一个 cookie 存储,通过设置域和路径值来创建一堆 cookie,并将这些添加到 cookie 存储中。import org.apache.http.client.CookieStore; import org.apache.http.impl.client.BasicCookieStore; import org.apache.http.impl.cookie.BasicClientCookie; public class CookieHandlingExample { public static void main(String args[]) throws Exception{ //Creating the CookieStore object CookieStore cookiestore = new BasicCookieStore(); //Creating client cookies BasicClientCookie clientcookie1 = new BasicClientCookie("name","Raju"); BasicClientCookie clientcookie2 = new BasicClientCookie("age","28"); BasicClientCookie clientcookie3 = new BasicClientCookie("place","Hyderabad"); //Setting domains and paths to the created cookies clientcookie1.setDomain(".sample.com"); clientcookie2.setDomain(".sample.com"); clientcookie3.setDomain(".sample.com"); clientcookie1.setPath("/"); clientcookie2.setPath("/"); clientcookie3.setPath("/"); //Adding the created cookies to cookie store cookiestore.addCookie(clientcookie1); cookiestore.addCookie(clientcookie2); cookiestore.addCookie(clientcookie3); } }
-
获取cookie
您可以使用 asicCookieStore 类的 getCookies() 方法将 cookie 添加到 cookie 存储中。此方法返回一个列表,其中包含 cookie 存储中的所有 cookie。您可以使用 Iterator 打印 cookie 存储的内容,如下所示 -//Retrieving the cookies List list = cookieStore.getCookies(); //Creating an iterator to the obtained list Iterator it = list.iterator(); while(it.hasNext()) { System.out.println(it.next()); }
-
示例
以下示例演示了如何从 cookie 存储中检索 cookie。在这里,我们将一堆 cookie 添加到 cookie 存储中并取回它们。import org.apache.http.client.CookieStore; import org.apache.http.impl.client.BasicCookieStore; import org.apache.http.impl.cookie.BasicClientCookie; public class CookieHandlingExample { public static void main(String args[]) throws Exception{ //Creating the CookieStore object CookieStore cookiestore = new BasicCookieStore(); //Creating client cookies BasicClientCookie clientcookie1 = new BasicClientCookie("name","Raju"); BasicClientCookie clientcookie2 = new BasicClientCookie("age","28"); BasicClientCookie clientcookie3 = new BasicClientCookie("place","Hyderabad"); //Setting domains and paths to the created cookies clientcookie1.setDomain(".sample.com"); clientcookie2.setDomain(".sample.com"); clientcookie3.setDomain(".sample.com"); clientcookie1.setPath("/"); clientcookie2.setPath("/"); clientcookie3.setPath("/"); //Adding the created cookies to cookie store cookiestore.addCookie(clientcookie1); cookiestore.addCookie(clientcookie2); cookiestore.addCookie(clientcookie3); } }
-
输出
在执行时,该程序生成以下输出 -[version: 0][name: age][value: 28][domain: .sample.com][path: /][expiry: null] [version: 0][name: name][value: Raju][domain: my.example.com][path: /][expiry: null] [version: 0][name: place][value: Hyderabad][domain: .sample.com][path: /][expiry: null]