android cookie持久化

简介: 原博客地址:http://blog.csdn.net/shimiso/article/details/39033353 在解析网页信息的时候,需要登录后才能访问,所以使用httpclient模拟登录,然后把cookie保存下来,以供下一次访问使用,这时就需要持久化cookie中的内容。 在之前先科普一下基础知识: 什么是Cookies?


原博客地址:http://blog.csdn.net/shimiso/article/details/39033353


在解析网页信息的时候,需要登录后才能访问,所以使用httpclient模拟登录,然后把cookie保存下来,以供下一次访问使用,这时就需要持久化cookie中的内容。

在之前先科普一下基础知识:

什么是Cookies?

Cookies是一些小文件,它们被创建在客户端的系统里,或者被创建在客户端浏览器的内存中(如果是临时性的话)。用它可以实现状态管理的功能。我们可以存储一些少量信息到可以短的系统上,以便在需要的时候使用。最有趣的事情是,它是对用户透明的。在你的web应用程序中,你可以到处使用它,它极其得简单。Cookies是以文本形式存储的。如果一个web应用程序使用cookies,那么服务器负责发送cookies,客户端浏览器将存储它。浏览器在下次请求页面的时候,会返回cookies给服务器。最常用的例子是,使用一个cookie来存储用户信息,用户的喜好,“记住密码”操作等。Cookies有许多优点,当然也有许多缺点。我将在接下来讲述。

Cookies是如何创建的?

当一个客户端向服务器发出请求,服务器发送cookies给客户端。而相同的cookies可以被后续的请求使用。例如,如果codeproject.com将Session ID作为cookies存储。当一个客户端首次向web服务器请求页面,服务器生成Session ID,并将其作为cookies发送往客户端。


现在,所有来自相同客户端的后续请求,它将使用来自cookies的Session ID,就像下面这幅图片展示的那样。


浏览器和web服务器以交换cookies信息来作为响应。对不同的站点,浏览器会维护不同的cookies。如果一个页面需要cookies中的信息,当某个URL被“点击”,首先浏览器将搜索本地系统的cookies的信息,然后才转向服务器来获得信息。

Cookies的优势

下面是使用cookies的主要优势:

(1)    实现和使用都是非常简单的

(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有一个失效时间的设置。

Cookie持久化

HttpClient可以和任意物理表示的实现了CookieStore接口的持久化cookie存储一起使用。默认的CookieStore实现称为BasicClientCookie,这是凭借java.util.ArrayList的一个简单实现。在BasicClientCookie对象中存储的cookie当容器对象被垃圾回收机制回收时会丢失。如果需要,用户可以提供更复杂的实现。


下载着重介绍在安卓中如何利用httpclient来实现对cookie的持久化操作:


一、请求网络获取cookie

先看一下下面的代码:

[java]  view plain copy
  1. DefaultHttpClient httpclient = new DefaultHttpClient();  
  2. HttpGet httpget = new HttpGet("http://www.hlovey.com");  
  3. HttpResponse response = httpclient.execute(httpget);  
  4. HttpEntity entity = response.getEntity();  
  5. List<Cookie> cookies = httpclient.getCookieStore().getCookies();  
Post模拟登录

[java]  view plain copy
  1. HttpPost httpPost = new HttpPost(url);  
  2. List<NameValuePair> formparams = new ArrayList<NameValuePair>();  
  3. formparams.add(new BasicNameValuePair("id", userid));  
  4. formparams.add(new BasicNameValuePair("passwd", passwd));  
  5. UrlEncodedFormEntity entity;  
  6. try {  
  7.     entity = new UrlEncodedFormEntity(formparams, mobileSMTHEncoding);  
  8. catch (UnsupportedEncodingException e1) {  
  9.     return 3;  
  10. }  
  11. httpPost.setEntity(entity);  
  12. httpPost.setHeader("User-Agent", userAgent);  
  13. HttpResponse response = httpClient.execute(httpPost);  
二、保存cookie

保存cookie有两种方式一种是数据库,另一种是SharedPreferences,其中http://blog.csdn.net/junjieking/article/details/7658551是使用数据库来保存的,这里我是使用SharedPreferences保存。

[java]  view plain copy
  1.     package com.smthbest.smth.util;  
  2.   
  3.     import java.util.Locale;  
  4.     import android.content.Context;  
  5.     import android.content.SharedPreferences;  
  6.     import android.text.TextUtils;  
  7.     import android.util.Log;  
  8.   
  9.     import org.apache.http.client.CookieStore;  
  10.     import org.apache.http.cookie.Cookie;  
  11.   
  12.     import java.io.ByteArrayInputStream;  
  13.     import java.io.ByteArrayOutputStream;  
  14.     import java.io.ObjectInputStream;  
  15.     import java.io.ObjectOutputStream;  
  16.     import java.util.ArrayList;  
  17.     import java.util.Date;  
  18.     import java.util.List;  
  19.     import java.util.Locale;  
  20.     import java.util.concurrent.ConcurrentHashMap;  
  21.   
  22. ic class PersistentCookieStore implements CookieStore {  
  23. private static final String LOG_TAG = "PersistentCookieStore";  
  24. private static final String COOKIE_PREFS = "CookiePrefsFile";  
  25. private static final String COOKIE_NAME_STORE = "names";  
  26. private static final String COOKIE_NAME_PREFIX = "cookie_";  
  27. private boolean omitNonPersistentCookies = false;  
  28.   
  29. private final ConcurrentHashMap<String, Cookie> cookies;  
  30. private final SharedPreferences cookiePrefs;  
  31.   
  32. /** 
  33.  * Construct a persistent cookie store. 
  34.  * 
  35.  * @param context Context to attach cookie store to 
  36.  */  
  37. public PersistentCookieStore(Context context) {  
  38.     cookiePrefs = context.getSharedPreferences(COOKIE_PREFS, 0);  
  39.     cookies = new ConcurrentHashMap<String, Cookie>();  
  40.   
  41.     // Load any previously stored cookies into the store  
  42.     String storedCookieNames = cookiePrefs.getString(COOKIE_NAME_STORE, null);  
  43.     if (storedCookieNames != null) {  
  44.         String[] cookieNames = TextUtils.split(storedCookieNames, ",");  
  45.         for (String name : cookieNames) {  
  46.             String encodedCookie = cookiePrefs.getString(COOKIE_NAME_PREFIX + name, null);  
  47.             if (encodedCookie != null) {  
  48.                 Cookie decodedCookie = decodeCookie(encodedCookie);  
  49.                 if (decodedCookie != null) {  
  50.                     cookies.put(name, decodedCookie);  
  51.                 }  
  52.             }  
  53.         }  
  54.   
  55.         // Clear out expired cookies  
  56.         clearExpired(new Date());  
  57.     }  
  58. }  
  59.   
  60. @Override  
  61. public void addCookie(Cookie cookie) {  
  62.     if (omitNonPersistentCookies && !cookie.isPersistent())  
  63.         return;  
  64.     String name = cookie.getName() + cookie.getDomain();  
  65.   
  66.     // Save cookie into local store, or remove if expired  
  67.     if (!cookie.isExpired(new Date())) {  
  68.         cookies.put(name, cookie);  
  69.     } else {  
  70.         cookies.remove(name);  
  71.     }  
  72.   
  73.     // Save cookie into persistent store  
  74.     SharedPreferences.Editor prefsWriter = cookiePrefs.edit();  
  75.     prefsWriter.putString(COOKIE_NAME_STORE, TextUtils.join(",", cookies.keySet()));  
  76.     prefsWriter.putString(COOKIE_NAME_PREFIX + name, encodeCookie(new SerializableCookie(cookie)));  
  77.     prefsWriter.commit();  
  78. }  
  79.   
  80. @Override  
  81. public void clear() {  
  82.     // Clear cookies from persistent store  
  83.     SharedPreferences.Editor prefsWriter = cookiePrefs.edit();  
  84.     for (String name : cookies.keySet()) {  
  85.         prefsWriter.remove(COOKIE_NAME_PREFIX + name);  
  86.     }  
  87.     prefsWriter.remove(COOKIE_NAME_STORE);  
  88.     prefsWriter.commit();  
  89.   
  90.     // Clear cookies from local store  
  91.     cookies.clear();  
  92. }  
  93.   
  94. @Override  
  95. public boolean clearExpired(Date date) {  
  96.     boolean clearedAny = false;  
  97.     SharedPreferences.Editor prefsWriter = cookiePrefs.edit();  
  98.   
  99.     for (ConcurrentHashMap.Entry<String, Cookie> entry : cookies.entrySet()) {  
  100.         String name = entry.getKey();  
  101.         Cookie cookie = entry.getValue();  
  102.         if (cookie.isExpired(date)) {  
  103.             // Clear cookies from local store  
  104.             cookies.remove(name);  
  105.   
  106.             // Clear cookies from persistent store  
  107.             prefsWriter.remove(COOKIE_NAME_PREFIX + name);  
  108.   
  109.             // We've cleared at least one  
  110.             clearedAny = true;  
  111.         }  
  112.     }  
  113.   
  114.     // Update names in persistent store  
  115.     if (clearedAny) {  
  116.         prefsWriter.putString(COOKIE_NAME_STORE, TextUtils.join(",", cookies.keySet()));  
  117.     }  
  118.     prefsWriter.commit();  
  119.   
  120.     return clearedAny;  
  121. }  
  122.   
  123. @Override  
  124. public List<Cookie> getCookies() {  
  125.     return new ArrayList<Cookie>(cookies.values());  
  126. }  
  127.   
  128. /** 
  129.  * Will make PersistentCookieStore instance ignore Cookies, which are non-persistent by 
  130.  * signature (`Cookie.isPersistent`) 
  131.  * 
  132.  * @param omitNonPersistentCookies true if non-persistent cookies should be omited 
  133.  */  
  134. public void setOmitNonPersistentCookies(boolean omitNonPersistentCookies) {  
  135.     this.omitNonPersistentCookies = omitNonPersistentCookies;  
  136. }  
  137.   
  138. /** 
  139.  * Non-standard helper method, to delete cookie 
  140.  * 
  141.  * @param cookie cookie to be removed 
  142.  */  
  143. public void deleteCookie(Cookie cookie) {  
  144.     String name = cookie.getName();  
  145.     cookies.remove(name);  
  146.     SharedPreferences.Editor prefsWriter = cookiePrefs.edit();  
  147.     prefsWriter.remove(COOKIE_NAME_PREFIX + name);  
  148.     prefsWriter.commit();  
  149. }  
  150.   
  151. /** 
  152.  * Serializes Cookie object into String 
  153.  * 
  154.  * @param cookie cookie to be encoded, can be null 
  155.  * @return cookie encoded as String 
  156.  */  
  157. protected String encodeCookie(SerializableCookie cookie) {  
  158.     if (cookie == null)  
  159.         return null;  
  160.     ByteArrayOutputStream os = new ByteArrayOutputStream();  
  161.     try {  
  162.         ObjectOutputStream outputStream = new ObjectOutputStream(os);  
  163.         outputStream.writeObject(cookie);  
  164.     } catch (Exception e) {  
  165.         return null;  
  166.     }  
  167.   
  168.     return byteArrayToHexString(os.toByteArray());  
  169. }  
  170.   
  171. /** 
  172.  * Returns cookie decoded from cookie string 
  173.  * 
  174.  * @param cookieString string of cookie as returned from http request 
  175.  * @return decoded cookie or null if exception occured 
  176.  */  
  177. protected Cookie decodeCookie(String cookieString) {  
  178.     byte[] bytes = hexStringToByteArray(cookieString);  
  179.     ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);  
  180.     Cookie cookie = null;  
  181.     try {  
  182.         ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream);  
  183.         cookie = ((SerializableCookie) objectInputStream.readObject()).getCookie();  
  184.     } catch (Exception exception) {  
  185.         Log.d(LOG_TAG, "decodeCookie failed", exception);  
  186.     }  
  187.   
  188.     return cookie;  
  189. }  
  190.   
  191. /** 
  192.  * Using some super basic byte array <-> hex conversions so we don't have to rely on any 
  193.  * large Base64 libraries. Can be overridden if you like! 
  194.  * 
  195.  * @param bytes byte array to be converted 
  196.  * @return string containing hex values 
  197.  */  
  198. protected String byteArrayToHexString(byte[] bytes) {  
  199.     StringBuilder sb = new StringBuilder(bytes.length * 2);  
  200.     for (byte element : bytes) {  
  201.         int v = element & 0xff;  
  202.         if (v < 16) {  
  203.             sb.append('0');  
  204.         }  
  205.         sb.append(Integer.toHexString(v));  
  206.     }  
  207.     return sb.toString().toUpperCase(Locale.US);  
  208. }  
  209.   
  210. /** 
  211.  * Converts hex values from strings to byte arra 
  212.  * 
  213.  * @param hexString string of hex-encoded values 
  214.  * @return decoded byte array 
  215.  */  
  216. protected byte[] hexStringToByteArray(String hexString) {  
  217.     int len = hexString.length();  
  218.     byte[] data = new byte[len / 2];  
  219.     for (int i = 0; i < len; i += 2) {  
  220.         data[i / 2] = (byte) ((Character.digit(hexString.charAt(i), 16) << 4) + Character.digit(hexString.charAt(i + 1), 16));  
  221.     }  
  222.     return data;  
  223. }  
使用PersistentCookieStore来存储cookie,首先最好把 PersistentCookieStore放在Application获取其他的地方,取得唯一实例,保存cookie是在登录成功后,从下面代码获取保存。
[java]  view plain copy
  1. PersistentCookieStore myCookieStore = App.getInstance().getPersistentCookieStore();  
  2. List<Cookie> cookies = httpClient.getCookieStore().getCookies();  
  3. for (Cookie cookie:cookies){  
  4.     myCookieStore.addCookie(cookie);  
  5. }  
三、cookie的使用
[java]  view plain copy
  1. PersistentCookieStore cookieStore = new PersistentCookieStore(SmthBestApp.getInstance().getApplicationContext());  
  2. httpClient.setCookieStore(cookieStore);  
  3. HttpResponse response = httpClient.execute(httpget);  
这样就可以免再次登录了。
目录
相关文章
|
存储 Apache 容器
Cookie持久化方案——PersistentCookieStore源码解读。
客户端登陆之后一般都会在本地持有某个cookie,在退出登录时将这个cookie清理掉。如果Request的body体中持有这个cookie,服务器就会认为客户端的用户处于登录状态。反之,就会认为用户没有登录。 假设用户一直处于登录状态,如果他关闭了应用,那么他的登录状态应该保存起来。这样的话,在他下次打开应用时,他的状态还是登录状态,不需要再次登录。 如何实现呢?很简单,将有效的cookie保存起来,需要的时候拿出来,塞进请求里面就ok了。
Cookie持久化方案——PersistentCookieStore源码解读。
状态Cookie与持久化Cookie
状态Cookie与持久化Cookie和对应的实例
|
测试技术
接口测试平台代码实现121:cookie持久化-4
上节我们成功搞定了普通接口调用登陆态cookie保持会话的后台逻辑,本节我们要去搞用例库相关的逻辑。
接口测试平台代码实现121:cookie持久化-4
|
前端开发 测试技术
接口测试平台代码实现120:cookie持久化-3
接口测试平台代码实现120:cookie持久化-3
接口测试平台代码实现120:cookie持久化-3
|
前端开发 测试技术
接口测试平台代码实现119:cookie持久化-2
接口测试平台代码实现119:cookie持久化-2
接口测试平台代码实现119:cookie持久化-2
|
前端开发 测试技术
接口测试平台代码实现118:登陆态cookie持久化
我们在之前的章节中,成功搞定了登陆态的相关功能,但是我们之前设计的登陆态接口,其本质是提取返回值的特殊字段,然后插入到其他接口的url/header/body中。
|
存储
Retrofit+OkHttp实现Cookie持久化,RxJava方式
Retrofit+OkHttp实现Cookie持久化,RxJava方式
270 0
|
Android开发
Android WebView设置Cookie
Android WebView设置Cookie
323 0
|
Android开发
Android WebView获取cookie
Android WebView获取cookie
331 0
|
数据库 Android开发 数据库管理
Android使用Room持久化SQLite
原文链接 前言 Android中提供了SQLite数据库进行数据的持久化 ,并提供了对应API访问数据库,而Room框架提供了SQLite数据访问抽象层,为高效的数据库访问层带来便捷 APP可以缓存用户数据,当APP离线时便从SQLite读取数据,当重...
1723 0