Cookie是儲存在用戶端計算機上的文字檔案,用於各種資訊跟蹤目的。
HttpClient
可以為建立和管理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);
}
}
可以使用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]