在以前先科普一下基礎知識:java
什麼是Cookies?android
Cookies是一些小文件,它們被建立在客戶端的系統裏,或者被建立在客戶端瀏覽器的內存中(若是是臨時性的話)。用它能夠實現狀態管理的功能。咱們能夠存儲一些少許信息到能夠短的系統上,以便在須要的時候使用。最有趣的事情是,它是對用戶透明的。在你的web應用程序中,你能夠處處使用它,它極其得簡單。Cookies是以文本形式存儲的。若是一個web應用程序使用cookies,那麼服務器負責發送cookies,客戶端瀏覽器將存儲它。瀏覽器在下次請求頁面的時候,會返回cookies給服務器。最經常使用的例子是,使用一個cookie來存儲用戶信息,用戶的喜愛,「記住密碼」操做等。Cookies有許多優勢,固然也有許多缺點。我將在接下來說述。git
Cookies是如何建立的?web
當一個客戶端向服務器發出請求,服務器發送cookies給客戶端。而相同的cookies能夠被後續的請求使用。例如,若是codeproject.com將Session ID做爲cookies存儲。當一個客戶端首次向web服務器請求頁面,服務器生成Session ID,並將其做爲cookies發送往客戶端。
數據庫
如今,全部來自相同客戶端的後續請求,它將使用來自cookies的Session ID,就像下面這幅圖片展現的那樣。
apache
瀏覽器和web服務器以交換cookies信息來做爲響應。對不一樣的站點,瀏覽器會維護不一樣的cookies。若是一個頁面須要cookies中的信息,當某個URL被「點擊」,首先瀏覽器將搜索本地系統的cookies的信息,而後才轉向服務器來得到信息。瀏覽器
Cookies的優點安全
下面是使用cookies的主要優點:服務器
(1) 實現和使用都是很是簡單的cookie
(2) 由瀏覽器來負責維護髮送過來的數據(cookies內容)
(3) 對來自多個站點的cookies來說,瀏覽器自動管理它們
Cookies的劣勢
下面是cookies的主要劣勢:
(1) 它以簡單的文本格式來存儲數據,因此它一點也不安全
(2) 對於cookies數據,有大小限制(4kB)
(3) Cookies最大數目也有限制。主流瀏覽器提供將cookies的個數限制在20條。若是新cookies到來,那麼老的將被刪除。有些瀏覽器能支持到300條的cookies數。
(4) 咱們須要配置瀏覽器,cookies將不能工做在瀏覽器配置的高安全級別環境下。
什麼是持久化的和非持久化的Cookies
咱們能夠將cookies分紅兩類:
(1) 持久化的cookies
(2) 非持久化的cookies
持久化的cookies:這能夠被稱爲永久性的cookies,它被存儲在客戶端的硬盤內,直到它們失效。持久化的cookies應該被設置一個失效時間。有時,它們會一直存在直到用戶刪除它們。持久化的cookies一般被用來爲某個系統收集一個用戶的標識信息。
非持久化cookies:也能夠被稱之爲臨時性的cookies。若是沒有定義失效時間,那麼cookie將會被存儲在瀏覽器的內存中。我上面展現的例子就是一個非持久的cookies。
修改一個持久化的cookies與一個非持久化的cookies並無什麼不一樣。它們惟一的區別是——持久化的cookies有一個失效時間的設置。
HttpClient能夠和任意物理表示的實現了CookieStore接口的持久化cookie存儲一塊兒使用。默認的CookieStore實現稱爲BasicClientCookie,這是憑藉java.util.ArrayList的一個簡單實現。在BasicClientCookie對象中存儲的cookie當容器對象被垃圾回收機制回收時會丟失。若是須要,用戶能夠提供更復雜的實現。
下載着重介紹在安卓中如何利用httpclient來實現對cookie的持久化操做:
1、請求網絡獲取cookie
先看一下下面的代碼:
[java] view plaincopy
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet("http://www.hlovey.com");
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
List<Cookie> cookies = httpclient.getCookieStore().getCookies();
Post模擬登陸
[java] view plaincopy
HttpPost httpPost = new HttpPost(url);
List<NameValuePair> formparams = new ArrayList<NameValuePair>();
formparams.add(new BasicNameValuePair("id", userid));
formparams.add(new BasicNameValuePair("passwd", passwd));
UrlEncodedFormEntity entity;
try {
entity = new UrlEncodedFormEntity(formparams, mobileSMTHEncoding);
} catch (UnsupportedEncodingException e1) {
return 3;
}
httpPost.setEntity(entity);
httpPost.setHeader("User-Agent", userAgent);
HttpResponse response = httpClient.execute(httpPost);
2、保存cookie
保存cookie有兩種方式一種是數據庫,另外一種是SharedPreferences,其中http://blog.csdn.net/junjieking/article/details/7658551是使用數據庫來保存的,這裏我是使用SharedPreferences保存。
[java] view plaincopy
package com.smthbest.smth.util;
import java.util.Locale;
import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;
import android.util.Log;
import org.apache.http.client.CookieStore;
import org.apache.http.cookie.Cookie;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.concurrent.ConcurrentHashMap;
ic class PersistentCookieStore implements CookieStore {
private static final String LOG_TAG = "PersistentCookieStore";
private static final String COOKIE_PREFS = "CookiePrefsFile";
private static final String COOKIE_NAME_STORE = "names";
private static final String COOKIE_NAME_PREFIX = "cookie_";
private boolean omitNonPersistentCookies = false;
private final ConcurrentHashMap<String, Cookie> cookies;
private final SharedPreferences cookiePrefs;
/**
* Construct a persistent cookie store.
*
* @param context Context to attach cookie store to
*/
public PersistentCookieStore(Context context) {
cookiePrefs = context.getSharedPreferences(COOKIE_PREFS, 0);
cookies = new ConcurrentHashMap<String, Cookie>();
// Load any previously stored cookies into the store
String storedCookieNames = cookiePrefs.getString(COOKIE_NAME_STORE, null);
if (storedCookieNames != null) {
String[] cookieNames = TextUtils.split(storedCookieNames, ",");
for (String name : cookieNames) {
String encodedCookie = cookiePrefs.getString(COOKIE_NAME_PREFIX + name, null);
if (encodedCookie != null) {
Cookie decodedCookie = decodeCookie(encodedCookie);
if (decodedCookie != null) {
cookies.put(name, decodedCookie);
}
}
}
// Clear out expired cookies
clearExpired(new Date());
}
}
@Override
public void addCookie(Cookie cookie) {
if (omitNonPersistentCookies && !cookie.isPersistent())
return;
String name = cookie.getName() + cookie.getDomain();
// Save cookie into local store, or remove if expired
if (!cookie.isExpired(new Date())) {
cookies.put(name, cookie);
} else {
cookies.remove(name);
}
// Save cookie into persistent store
SharedPreferences.Editor prefsWriter = cookiePrefs.edit();
prefsWriter.putString(COOKIE_NAME_STORE, TextUtils.join(",", cookies.keySet()));
prefsWriter.putString(COOKIE_NAME_PREFIX + name, encodeCookie(new SerializableCookie(cookie)));
prefsWriter.commit();
}
@Override
public void clear() {
// Clear cookies from persistent store
SharedPreferences.Editor prefsWriter = cookiePrefs.edit();
for (String name : cookies.keySet()) {
prefsWriter.remove(COOKIE_NAME_PREFIX + name);
}
prefsWriter.remove(COOKIE_NAME_STORE);
prefsWriter.commit();
// Clear cookies from local store
cookies.clear();
}
@Override
public boolean clearExpired(Date date) {
boolean clearedAny = false;
SharedPreferences.Editor prefsWriter = cookiePrefs.edit();
for (ConcurrentHashMap.Entry<String, Cookie> entry : cookies.entrySet()) {
String name = entry.getKey();
Cookie cookie = entry.getValue();
if (cookie.isExpired(date)) {
// Clear cookies from local store
cookies.remove(name);
// Clear cookies from persistent store
prefsWriter.remove(COOKIE_NAME_PREFIX + name);
// We've cleared at least one
clearedAny = true;
}
}
// Update names in persistent store
if (clearedAny) {
prefsWriter.putString(COOKIE_NAME_STORE, TextUtils.join(",", cookies.keySet()));
}
prefsWriter.commit();
return clearedAny;
}
@Override
public List<Cookie> getCookies() {
return new ArrayList<Cookie>(cookies.values());
}
/**
* Will make PersistentCookieStore instance ignore Cookies, which are non-persistent by
* signature (`Cookie.isPersistent`)
*
* @param omitNonPersistentCookies true if non-persistent cookies should be omited
*/
public void setOmitNonPersistentCookies(boolean omitNonPersistentCookies) {
this.omitNonPersistentCookies = omitNonPersistentCookies;
}
/**
* Non-standard helper method, to delete cookie
*
* @param cookie cookie to be removed
*/
public void deleteCookie(Cookie cookie) {
String name = cookie.getName();
cookies.remove(name);
SharedPreferences.Editor prefsWriter = cookiePrefs.edit();
prefsWriter.remove(COOKIE_NAME_PREFIX + name);
prefsWriter.commit();
}
/**
* Serializes Cookie object into String
*
* @param cookie cookie to be encoded, can be null
* @return cookie encoded as String
*/
protected String encodeCookie(SerializableCookie cookie) {
if (cookie == null)
return null;
ByteArrayOutputStream os = new ByteArrayOutputStream();
try {
ObjectOutputStream outputStream = new ObjectOutputStream(os);
outputStream.writeObject(cookie);
} catch (Exception e) {
return null;
}
return byteArrayToHexString(os.toByteArray());
}
/**
* Returns cookie decoded from cookie string
*
* @param cookieString string of cookie as returned from http request
* @return decoded cookie or null if exception occured
*/
protected Cookie decodeCookie(String cookieString) {
byte[] bytes = hexStringToByteArray(cookieString);
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
Cookie cookie = null;
try {
ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream);
cookie = ((SerializableCookie) objectInputStream.readObject()).getCookie();
} catch (Exception exception) {
Log.d(LOG_TAG, "decodeCookie failed", exception);
}
return cookie;
}
/**
* Using some super basic byte array <-> hex conversions so we don't have to rely on any
* large Base64 libraries. Can be overridden if you like!
*
* @param bytes byte array to be converted
* @return string containing hex values
*/
protected String byteArrayToHexString(byte[] bytes) {
StringBuilder sb = new StringBuilder(bytes.length * 2);
for (byte element : bytes) {
int v = element & 0xff;
if (v < 16) {
sb.append('0');
}
sb.append(Integer.toHexString(v));
}
return sb.toString().toUpperCase(Locale.US);
}
/**
* Converts hex values from strings to byte arra
*
* @param hexString string of hex-encoded values
* @return decoded byte array
*/
protected byte[] hexStringToByteArray(String hexString) {
int len = hexString.length();
byte[] data = new byte[len / 2];
for (int i = 0; i < len; i += 2) {
data[i / 2] = (byte) ((Character.digit(hexString.charAt(i), 16) << 4) + Character.digit(hexString.charAt(i + 1), 16));
}
return data;
}
使用PersistentCookieStore來存儲cookie,首先最好把PersistentCookieStore放在Application獲取其餘的地方,取得惟一實例,保存cookie是在登陸成功後,從下面代碼獲取保存。
[java] view plaincopy
PersistentCookieStore myCookieStore = App.getInstance().getPersistentCookieStore();
List<Cookie> cookies = httpClient.getCookieStore().getCookies();
for (Cookie cookie:cookies){
myCookieStore.addCookie(cookie);
}
3、cookie的使用
[java] view plaincopy
PersistentCookieStore cookieStore = new PersistentCookieStore(SmthBestApp.getInstance().getApplicationContext());
httpClient.setCookieStore(cookieStore);
HttpResponse response = httpClient.execute(httpget);
這樣就能夠免再次登陸了。