Android 使用Okhttp/Retrofit持久化cookie的简便方式

赋料扬雄敌,诗看子建亲。这篇文章主要讲述Android 使用Okhttp/Retrofit持久化cookie的简便方式相关的知识,希望能为你提供帮助。
【Android 使用Okhttp/Retrofit持久化cookie的简便方式】首先cookie是什么就不多说了,还是不知道的话推荐看看这篇文章 
Cookie/Session机制详解 
深入解析Cookie技术
为什么要持久化cookie也不多说了,你能看到这篇文章代表你有这个需求。
cookie简单来说就是服务器在客户端中保存的键值对,比如说早期的购物车,保持登陆状态都是使用的cookie。 
但是cookie的功能是依赖于浏览器的,大多数浏览器都有管理cookie的功能。当然,你也能通过设置禁止掉这项功能,毕竟cookie是很容易泄露用户隐私的
上面也说了cookie功能依赖于客户端,很明显,在开发app的时候我们也要手动管理cookie了。
 

持久化cookie之前,我们最好了解一下cookie是怎么传输的,看完之后我想你就能使用很简单的方式去保持cookie了。

1. cookie是怎么传输的Cookie使用HTTPHeader传递数据。 
Cookie机制定义了两种报头:Set-Cookie报头和Cookie报头。
Set-Cookie报头包含于Web服务器的响应头(ResponseHeader)中 
Cookie报头包含在浏览器客户端请求头(ReguestHeader)中
Cookie的运行过程如图所示,具体分析如下 
Android 使用Okhttp/Retrofit持久化cookie的简便方式

文章图片

2. 简单粗暴持久化cookie的方式方法来源于这http://tsuharesu.com/handling-cookies-with-okhttp/
看了cookie的传输原理之后我们就可以用一个简单的方式持久化cookie了,那就是通过okhttp的intercept方式手动往header中写入cookie或者获取cookie。
/** * This interceptor put all the Cookies in Preferences in the Request. * Your implementation on how to get the Preferences MAY VARY. * < p> * Created by tsuharesu on 4/1/15. */ public class AddCookiesInterceptor implements Interceptor {@Override public Response intercept(Chain chain) throws IOException { Request.Builder builder = chain.request().newBuilder(); HashSet< String> preferences = (HashSet) Preferences.getDefaultPreferences().getStringSet(Preferences.PREF_COOKIES, new HashSet< > ()); for (String cookie : preferences) { builder.addHeader("Cookie", cookie); Log.v("OkHttp", "Adding Header: " + cookie); // This is done so I know which headers are being added; this interceptor is used after the normal logging of OkHttp }return chain.proceed(builder.build()); } }

/** * This Interceptor add all received Cookies to the app DefaultPreferences. * Your implementation on how to save the Cookies on the Preferences MAY VARY. * < p> * Created by tsuharesu on 4/1/15. */ public class ReceivedCookiesInterceptor implements Interceptor { @Override public Response intercept(Chain chain) throws IOException { Response originalResponse = chain.proceed(chain.request()); if (!originalResponse.headers("Set-Cookie").isEmpty()) { HashSet< String> cookies = new HashSet< > (); for (String header : originalResponse.headers("Set-Cookie")) { cookies.add(header); }Preferences.getDefaultPreferences().edit() .putStringSet(Preferences.PREF_COOKIES, cookies) .apply(); }return originalResponse; } }

/** * Somewhere you create a new OkHttpClient and use it on all your requests. */ OkHttpClient okHttpClient = new OkHttpClient(); okHttpClient.interceptors().add(new AddCookiesInterceptor()); okHttpClient.interceptors().add(new ReceivedCookiesInterceptor());

简单的cookie持久化可以用这种方式,创建一个单例的client,全局都使用这个client请求接口。当然你也可以每次new一个client重新设置intercept… …
还要记住的是,cookie是针对于域名存储的。比如:www.baidu.com和image.baidu.com存储的cookies都是不一样的… …  
如果你的app真的需要同时访问两个域名的接口,并且两个都需要持久化cookie,那么记得做判断(比如用域名作为key存储cookie到sharepreference中)。否则两个域名的cookie会互相覆盖掉… …
3.  本体在这里,okhttp 3.0 cookie的管理相关原理可以看看这篇文章  OkHttp3之Cookies管理及持久化
然后,持久化管理cookie可以直接使用这个开源库,简单到爆炸… …  
https://github.com/franmontiel/PersistentCookieJar
当然,你也可以使用鸿洋大神的okhttputils,该库也提供了cookie的持久化管理工具。用起来和上面一样方便

    推荐阅读