HttpClient Cookie管理
Cookies 是存储在客户端计算机上的文本文件,用于各种信息跟踪目的。
HttpClient 提供对 cookie 的支持,您可以创建和管理 cookie。
HttpClient 创建Cookie
按照下面给出的步骤使用 HttpClient 库创建 cookie。
1)创建 Cookiestore 对象
CookieStore接口代表了Cookie对象的抽象存储。您可以通过实例化BasicCookieStore类(此接口的默认实现)来创建 cookie 存储。
//Creating the CookieStore object
CookieStore cookieStore = new BasicCookieStore();
2)创建 ClientCookie 对象
除了 cookie 的功能外,ClientCookie 还可以获取服务器中的原始 cookie。您可以通过实例化BasicClientCookie类来创建客户端 cookie 。对于此类的构造函数,您需要传递您希望存储在该特定 cookie 中的键值对。
//Creating client cookie
BasicClientCookie clientCookie = new BasicClientCookie("name","jack");
3)为 cookie 设置值
对于客户端 cookie,您可以使用相应的方法设置/删除路径、值、版本、到期日期、域、注释和属性。
Calendar myCal = new GregorianCalendar(2021, 8, 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。
//Adding the created cookies to cookie store
cookiestore.addCookie(clientcookie);
HttpClient 创建Cookie的完整例子
package com.yiidian;
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","jack");
BasicClientCookie clientcookie2 = new BasicClientCookie("age","28");
BasicClientCookie clientcookie3 = new BasicClientCookie("place","guangzhou");
//Setting domains and paths to the created cookies
clientcookie1.setDomain(".yiidian.com");
clientcookie2.setDomain(".yiidian.com");
clientcookie3.setDomain(".yiidian.com");
clientcookie1.setPath("/");
clientcookie2.setPath("/");
clientcookie3.setPath("/");
//Adding the created cookies to cookie store
cookiestore.addCookie(clientcookie1);
cookiestore.addCookie(clientcookie2);
cookiestore.addCookie(clientcookie3);
}
}
HttpClient 获取Cookie
您可以使用asicCookieStore类的getCookies()方法将 cookie 添加到 cookie 存储中。此方法返回一个列表,其中包含 cookie 存储中的所有 cookie。
您可以使用迭代器打印 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());
}
HttpClient 获取Cookie的完整例子
以下示例演示了如何从 cookie 存储中获取 cookie。在这里,我们将一堆 cookie 添加到 cookie 存储并获取它们。
package com.yiidian;
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","jack");
BasicClientCookie clientcookie2 = new BasicClientCookie("age","28");
BasicClientCookie clientcookie3 = new BasicClientCookie("place","gaungzhou");
//Setting domains and paths to the created cookies
clientcookie1.setDomain(".yiidian.com");
clientcookie2.setDomain(".yiidian.com");
clientcookie3.setDomain(".yiidian.com");
clientcookie1.setPath("/");
clientcookie2.setPath("/");
clientcookie3.setPath("/");
//Adding the created cookies to cookie store
cookiestore.addCookie(clientcookie1);
cookiestore.addCookie(clientcookie2);
cookiestore.addCookie(clientcookie3);
}
}
热门文章
优秀文章