📅  最后修改于: 2020-11-18 08:30:56             🧑  作者: Mango
Cookies是存储在客户端计算机上的文本文件,它们被保留以用于各种信息跟踪目的。
HttpClient提供对cookie的支持,您可以创建和管理cookie。
请按照下面给出的步骤使用HttpClient库创建cookie。
CookieStore接口表示Cookie对象的抽象存储。您可以通过实例化BasicCookieStore类(此接口的默认实现)来创建cookie存储。
//Creating the CookieStore object
CookieStore cookieStore = new BasicCookieStore();
除了cookie的功能外,ClientCookie还可在服务器中获取原始cookie。您可以通过实例化BasicClientCookie类来创建客户端cookie。对于此类的构造方法,您需要将要存储在该特定cookie中的键值对传递。
//Creating client cookie
BasicClientCookie clientCookie = new BasicClientCookie("name","Raju");
对于客户端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);
您可以使用BasicCookieStore类的addCookie()方法将cookie添加到cookie存储中。
将所需的cookie添加到Cookiestore中。
//Adding the created cookies to 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);
}
}
您可以使用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]