Android持久化保存cookie的方法
在解析網(wǎng)頁信息的時(shí)候,需要登錄后才能訪問,所以使用httpclient模擬登錄,然后把cookie保存下來,以供下一次訪問使用,這時(shí)就需要持久化cookie中的內(nèi)容。
在之前先科普一下基礎(chǔ)知識(shí):
什么是Cookies?
Cookies是一些小文件,它們被創(chuàng)建在客戶端的系統(tǒng)里,或者被創(chuàng)建在客戶端瀏覽器的內(nèi)存中(如果是臨時(shí)性的話)。用它可以實(shí)現(xiàn)狀態(tài)管理的功能。我們可以存儲(chǔ)一些少量信息到可以短的系統(tǒng)上,以便在需要的時(shí)候使用。最有趣的事情是,它是對(duì)用戶透明的。在你的web應(yīng)用程序中,你可以到處使用它,它極其得簡(jiǎn)單。Cookies是以文本形式存儲(chǔ)的。如果一個(gè)web應(yīng)用程序使用cookies,那么服務(wù)器負(fù)責(zé)發(fā)送cookies,客戶端瀏覽器將存儲(chǔ)它。瀏覽器在下次請(qǐng)求頁面的時(shí)候,會(huì)返回cookies給服務(wù)器。最常用的例子是,使用一個(gè)cookie來存儲(chǔ)用戶信息,用戶的喜好,“記住密碼”操作等。Cookies有許多優(yōu)點(diǎn),當(dāng)然也有許多缺點(diǎn)。我將在接下來講述。
Cookies是如何創(chuàng)建的?
當(dāng)一個(gè)客戶端向服務(wù)器發(fā)出請(qǐng)求,服務(wù)器發(fā)送cookies給客戶端。而相同的cookies可以被后續(xù)的請(qǐng)求使用。例如,如果codeproject.com將Session ID作為cookies存儲(chǔ)。當(dāng)一個(gè)客戶端首次向web服務(wù)器請(qǐng)求頁面,服務(wù)器生成Session ID,并將其作為cookies發(fā)送往客戶端。
現(xiàn)在,所有來自相同客戶端的后續(xù)請(qǐng)求,它將使用來自cookies的Session ID,就像下面這幅圖片展示的那樣。
瀏覽器和web服務(wù)器以交換cookies信息來作為響應(yīng)。對(duì)不同的站點(diǎn),瀏覽器會(huì)維護(hù)不同的cookies。如果一個(gè)頁面需要cookies中的信息,當(dāng)某個(gè)URL被“點(diǎn)擊”,首先瀏覽器將搜索本地系統(tǒng)的cookies的信息,然后才轉(zhuǎn)向服務(wù)器來獲得信息。
Cookies的優(yōu)勢(shì)
下面是使用cookies的主要優(yōu)勢(shì):
(1) 實(shí)現(xiàn)和使用都是非常簡(jiǎn)單的
(2) 由瀏覽器來負(fù)責(zé)維護(hù)發(fā)送過來的數(shù)據(jù)(cookies內(nèi)容)
(3) 對(duì)來自多個(gè)站點(diǎn)的cookies來講,瀏覽器自動(dòng)管理它們
Cookies的劣勢(shì)
下面是cookies的主要劣勢(shì):
(1) 它以簡(jiǎn)單的文本格式來存儲(chǔ)數(shù)據(jù),所以它一點(diǎn)也不安全
(2) 對(duì)于cookies數(shù)據(jù),有大小限制(4kB)
(3) Cookies最大數(shù)目也有限制。主流瀏覽器提供將cookies的個(gè)數(shù)限制在20條。如果新cookies到來,那么老的將被刪除。有些瀏覽器能支持到300條的cookies數(shù)。
(4) 我們需要配置瀏覽器,cookies將不能工作在瀏覽器配置的高安全級(jí)別環(huán)境下。
什么是持久化的和非持久化的Cookies
我們可以將cookies分成兩類:
(1) 持久化的cookies
(2) 非持久化的cookies
持久化的cookies:這可以被稱為永久性的cookies,它被存儲(chǔ)在客戶端的硬盤內(nèi),直到它們失效。持久化的cookies應(yīng)該被設(shè)置一個(gè)失效時(shí)間。有時(shí),它們會(huì)一直存在直到用戶刪除它們。持久化的cookies通常被用來為某個(gè)系統(tǒng)收集一個(gè)用戶的標(biāo)識(shí)信息。
非持久化cookies:也可以被稱之為臨時(shí)性的cookies。如果沒有定義失效時(shí)間,那么cookie將會(huì)被存儲(chǔ)在瀏覽器的內(nèi)存中。我上面展示的例子就是一個(gè)非持久的cookies。
修改一個(gè)持久化的cookies與一個(gè)非持久化的cookies并沒有什么不同。它們唯一的區(qū)別是——持久化的cookies有一個(gè)失效時(shí)間的設(shè)置。
Cookie持久化
HttpClient可以和任意物理表示的實(shí)現(xiàn)了CookieStore接口的持久化cookie存儲(chǔ)一起使用。默認(rèn)的CookieStore實(shí)現(xiàn)稱為BasicClientCookie,這是憑借java.util.ArrayList的一個(gè)簡(jiǎn)單實(shí)現(xiàn)。在BasicClientCookie對(duì)象中存儲(chǔ)的cookie當(dāng)容器對(duì)象被垃圾回收機(jī)制回收時(shí)會(huì)丟失。如果需要,用戶可以提供更復(fù)雜的實(shí)現(xiàn)。
下載著重介紹在安卓中如何利用httpclient來實(shí)現(xiàn)對(duì)cookie的持久化操作:
一、請(qǐng)求網(wǎng)絡(luò)獲取cookie
先看一下下面的代碼:
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模擬登錄
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);
二、保存cookie
保存cookie有兩種方式一種是數(shù)據(jù)庫,另一種是SharedPreferences,其中http://www.dbjr.com.cn/article/140423.htm是使用數(shù)據(jù)庫來保存的,這里我是使用SharedPreferences保存。
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來存儲(chǔ)cookie,首先最好把PersistentCookieStore放在Application獲取其他的地方,取得唯一實(shí)例,保存cookie是在登錄成功后,從下面代碼獲取保存。
PersistentCookieStore myCookieStore = App.getInstance().getPersistentCookieStore(); List<Cookie> cookies = httpClient.getCookieStore().getCookies(); for (Cookie cookie:cookies){ myCookieStore.addCookie(cookie); }
三、cookie的使用
PersistentCookieStore cookieStore = new PersistentCookieStore(SmthBestApp.getInstance().getApplicationContext()); httpClient.setCookieStore(cookieStore); HttpResponse response = httpClient.execute(httpget);
這樣就可以免再次登錄了。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android實(shí)現(xiàn)View滑動(dòng)的6種方式
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)View滑動(dòng)的6種方式,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05詳解Android如何實(shí)現(xiàn)自定義的動(dòng)畫曲線
最近在寫動(dòng)畫相關(guān)的篇章,經(jīng)常會(huì)用到 Curve 這個(gè)動(dòng)畫曲線類,那這個(gè)類到底怎么實(shí)現(xiàn)的?如果想自己來一個(gè)自定義的動(dòng)畫曲線該怎么弄?本文將為大家詳細(xì)解答2022-04-04Android GridView簡(jiǎn)單實(shí)例
這篇文章主要為大家詳細(xì)介紹了Android GridView簡(jiǎn)單實(shí)例,簡(jiǎn)單實(shí)現(xiàn)九宮格效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01Android開發(fā)中PopupWindow用法實(shí)例分析
這篇文章主要介紹了Android開發(fā)中PopupWindow用法,結(jié)合實(shí)例形式分析了PopupWindow彈出窗口效果的使用技巧,需要的朋友可以參考下2016-02-02Android實(shí)現(xiàn)帶有進(jìn)度條的按鈕效果
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)帶有進(jìn)度條的按鈕效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-05-05Android.mk引入第三方j(luò)ar包和so庫文件的方法
這篇文章主要介紹了Android.mk引入第三方j(luò)ar包和so庫文件的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-05-05Android仿QQ、新浪相冊(cè)的實(shí)現(xiàn)
這篇文章主要為大家詳細(xì)介紹了Android仿QQ、新浪相冊(cè)的實(shí)現(xiàn)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11android實(shí)現(xiàn)關(guān)閉或開啟移動(dòng)網(wǎng)絡(luò)數(shù)據(jù)
本篇文章是對(duì)android實(shí)現(xiàn)關(guān)閉或開啟移動(dòng)網(wǎng)絡(luò)數(shù)據(jù)進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06