Android之Cookie在開發中應用
阿新 • • 發佈:2019-01-11
Cookie介紹-來自百度百科
Cookie,有時也用其複數形式 Cookies,指某些網站為了辨別使用者身份、進行 session 跟蹤而儲存在使用者本地終端上的資料(通常經過加密)。定義於 RFC2109 和 2965 中的都已廢棄,最新取代的規範是 RFC6265[1] 。(可以叫做瀏覽器快取)
Cookie可以保持登入資訊到使用者下次與伺服器的會話,換句話說,下次訪問同一網站時,使用者會發現不必輸入使用者名稱和密碼就已經登入了(當然,不排除使用者手工刪除Cookie)。而還有一些Cookie在使用者退出會話的時候就被刪除了,這樣可以有效保護個人隱私。
Cookie在生成時就會被指定一個Expire值,這就是Cookie的生存週期,在這個週期內Cookie有效,超出週期Cookie就會被清除。有些頁面將Cookie的生存週期設定為“0”或負值,這樣在關閉瀏覽器時,就馬上清除Cookie,不會記錄使用者資訊,更加安全。
Cookie在Android的應用
- 為了減輕服務端的壓力,有時候在登入過後,服務端會為客戶端生成一個cookie,當客戶端想要獲取登入後的資訊,不必每次請求都攜帶使用者名稱和密碼,只需要帶上cookie,當cookie超期時,會獲取到相應的信心,提示使用者重新進行登入
- cookie會有不安全性,有可能被擷取,然後模擬登入狀態獲取資訊
程式碼演示
- 模擬登入,獲取cookie,抓的包模擬–
//登入按鈕
public void login(View v) {
new Thread() {
public void run() {
try {
//建立HttpClient物件
DefaultHttpClient defaultHttpClient = new DefaultHttpClient();
//建立httpPost物件,指定請求地址
HttpPost httpPost = new HttpPost(
"http://www.meirixue.com/api.php?c=login&a=index");
//建立集合的鍵值對
List<BasicNameValuePair> parameters = new ArrayList<BasicNameValuePair>();
//新增使用者名稱和密碼----這裡的使用者名稱大家可以下載每日學客戶端註冊一個--抓包抓的測試介面,使用者名稱和密碼不能給大家
parameters.add(new BasicNameValuePair("userName",
"xxxxxxxxxx"));
parameters.add(new BasicNameValuePair("password",
"xxxxxxxxxx"));
parameters.add(new BasicNameValuePair("dosubmit", "1"));
// 新增引數
httpPost.setEntity(new UrlEncodedFormEntity(parameters));
//執行請求
HttpResponse httpResponse = defaultHttpClient
.execute(httpPost);
int code = httpResponse.getStatusLine().getStatusCode();
//判斷一下響應碼
if (code == 200) {
//獲取網路流
InputStream inputStream = httpResponse.getEntity()
.getContent();
//將流轉換成字串
String parseStream = StreamUtils
.parseStream(inputStream);
System.out.println("---" + parseStream);
// 獲取cookie---這種方式是HttpClient專屬方式,每種方式是不同的
CookieStore cookieStore = defaultHttpClient
.getCookieStore();
List<Cookie> cookies = cookieStore.getCookies();
//進行拼接--這裡拼接根據公司是不同的,這裡的拼接效果是 key=value;key=value;
StringBuffer sb = new StringBuffer();
for (int i = 0; i < cookies.size(); i++) {
Cookie cookie = cookies.get(i);
String cookieName = cookie.getName();
String cookieValue = cookie.getValue();
if (!TextUtils.isEmpty(cookieName)
&& !TextUtils.isEmpty(cookieValue)) {
sb.append(cookieName + "=");
sb.append(cookieValue + ";");
}
}
// 存到檔案中
SharedPreferencesUtils.saveString(MainActivity.this,
"cookie", sb.toString());
}
} catch (Exception e) {
e.printStackTrace();
}
};
}.start();
}
- 登入之後,獲取到這個cookie並存儲到本地,再獲取一些個人資訊時,攜帶者cookie去做請求
/**
* 查詢課程資訊--需要在登入結束之後,獲取到的cookie並儲存到本地之後,獲取cookie資訊,作為請求頭
*
* @param v
*/
public void queryInfo(View v) {
new Thread() {
public void run() {
try {
//建立客戶端物件
DefaultHttpClient defaultHttpClient = new DefaultHttpClient();
//這個地址是進行個人資料的請求,前提是先完成了登入
HttpPost httpPost = new HttpPost(
"http://www.meirixue.com/api.php?c=notice&a=index");
// 新增cookie
httpPost.addHeader("Cookie", SharedPreferencesUtils
.getString(MainActivity.this, "cookie", ""));
HttpResponse httpResponse = defaultHttpClient
.execute(httpPost);
int code = httpResponse.getStatusLine().getStatusCode();
//如果請求成功,並且成功碼是200代表是已經成功了,如果是205,代表未登入的狀態
if (code == 200) {
InputStream inputStream = httpResponse.getEntity()
.getContent();
String parseStream = StreamUtils
.parseStream(inputStream);
System.out.println("---" + parseStream);
} else {
System.out.println("---" + code);
}
} catch (Exception e) {
e.printStackTrace();
}
};
}.start();
}
以上就是對Cookie的程式碼演示…是基於Android的HttpClient來做的模擬,如果想要從其他網路框架中使用,需要用其他方式
這篇文章就對Retrofit中使用cookie做了講解
http://blog.csdn.net/zhiyuan0932/article/details/54318944