HttpClient Cookies 管理

Cookie是存储在客户端计算机上的文本文件,用于各种信息跟踪目的。

HttpClient可以为创建和管理cookie提供支持。

创建一个cookie

下面给出的步骤使用HttpClient库创建cookie

第1步-创建Cookiestore对象

CookieStore接口表示Cookie对象的抽象存储。可以通过实例化BasicCookieStore类来创建cookie存储,BasicCookieStore类是这个接口的默认实现。

//Creating the CookieStore object
CookieStore cookieStore = new BasicCookieStore();

爪哇

第2步-创建ClientCookie对象

除了cookie的功能之外,ClientCookie还可以在服务器中获取原始cookie。可以通过实例化BasicClientCookie类来创建客户端cookie。对于此类构造函数,需要传递要在特定cookie中存储的键值对。

//Creating client cookie
BasicClientCookie clientCookie = new BasicClientCookie("name","maxsu");

爪哇

第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添加到存储区

可以使用BasicCookieStore类的addCookie()方法将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","Maxsu");
      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。

使用迭代器可以打印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","Maxsu");
      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: Maxsu][domain: my.example.com][path: /][expiry:
null]
[version: 0][name: place][value: Hyderabad][domain: .sample.com][path:
/][expiry: null]