本篇文章為大家展示了怎么在Android中持久化保存cookie,內(nèi)容簡(jiǎn)明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過這篇文章的詳細(xì)介紹希望你能有所收獲。
創(chuàng)新互聯(lián)主要從事網(wǎng)站制作、成都網(wǎng)站設(shè)計(jì)、網(wǎng)頁(yè)設(shè)計(jì)、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)霞浦,十年網(wǎng)站建設(shè)經(jīng)驗(yàn),價(jià)格優(yōu)惠、服務(wù)專業(yè),歡迎來電咨詢建站服務(wù):18980820575
什么是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)求頁(yè)面的時(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)求頁(yè)面,服務(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è)頁(yè)面需要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(); Listcookies = httpclient.getCookieStore().getCookies();
Post模擬登錄
HttpPost httpPost = new HttpPost(url); Listformparams = new ArrayList (); 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ù)庫(kù),另一種是SharedPreferences,其中https://www.jb51.net/article/140423.htm是使用數(shù)據(jù)庫(kù)來保存的,這里我是使用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 ConcurrentHashMapcookies; 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 (); // 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 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 getCookies() { return new ArrayList (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(); Listcookies = 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);
Android是一種基于Linux內(nèi)核的自由及開放源代碼的操作系統(tǒng),主要使用于移動(dòng)設(shè)備,如智能手機(jī)和平板電腦,由美國(guó)Google公司和開放手機(jī)聯(lián)盟領(lǐng)導(dǎo)及開發(fā)。
上述內(nèi)容就是怎么在Android中持久化保存cookie,你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。