獲取Cookies的方法
public void testGetCookies() throws IOException {
String result;
// 從組態檔中 拼接測驗的url
String uri = bundle.getString("getCookies.uri");
String testUrl = this.url + uri;
// 測驗邏輯代碼書寫
HttpGet get = new HttpGet(testUrl);
DefaultHttpClient client = new DefaultHttpClient();
HttpResponse response = client.execute(get);
result = EntityUtils.toString(response.getEntity(), "utf-8");
System.out.println(result);
//獲取cookies資訊
this.store = client.getCookieStore();
List<Cookie> cookieList = store.getCookies();
for (Cookie cookie : cookieList) {
String name = cookie.getName();
String value = cookie.getValue();
System.out.println("cookie name = " + name
+ "; cookie value = " + value);
}
}
這是一個獲取cookie的方法,我想問一下我標用的這兩個代碼為什么不是通過response.getCookieStore()來獲取cookie資訊而是用clent.getCookieStore()獲取呢?response是訪問服務器介面回傳來才能獲得cookie呀,而clent這個物件并沒有接收到服務器回傳來的資訊,它只是DefaultHttpClient new出來的物件負責執行發送請求而已,應該沒有cookie才對呀 為什么能獲取到服務器回傳來的cookie資訊呢
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/114723.html
標籤:Java EE
