Retrofit+OKHttp 教你怎么持久化管理Cookie

原创 2016年05月08日 13:46:43

绪论

最近小编有点忙啊,项目比较紧,所以一直在忙活项目,继之前的自定义组件之后就没再写博客了,如果你没看到之前的自定义组件你可以看一下:
Android自定义下拉刷新动画–仿百度外卖下拉刷新
Android自定义组合控件—教你如何自定义下拉刷新和左滑删除
效果还行,源码也已经传到我的Github上了。
那么今天小编来给大家分享点什么呢?对,就是它:Retrofit,话说Retrofit最近真的很火啊,Retrofit+OKHttp现在似乎已经成为了Android网络请求框架的主流框架了吧,小编之前用的是XUtils框架,个人感觉也不错,也更新到了Xutils3,但是毕竟Retrofit是Square出的,所以小编还是忍不住需要探索一下。
鉴于现在Retrofit现在网上很多教程,所以基本的使用方法就不介绍了,小编也不重复造轮子了,如果你还不会用,看看下面几篇文章:
Retrofit 2.0使用详解,配合OkHttp、Gson,Android最强网络请求框架
Retrofit 2.0:有史以来最大的改进
Retrofit初探和简单使用

持久化Cookie

今天小编要讲的是,怎么持久化管理你的Cookie,也就是实现用户免登陆过程。
首先说一下需求,后台大哥哥是这样告诉我的:我们的用户登录需要你在本地管理cookie,用户下次进来的时候不需要再登录,调用其他接口的时候将用户的cookie和session放到请求头里面。我果断的答应了,因为之前用Xutils的时候也这么做过,当我去网上找资料的时候发现并没有很好的资料。因为Retrofit内部是Ok来实现的,所以方向可以找到Ok管理Cookie,好了,方向找到了,我们来看一下OKHttp:
OKHttp3.0之前和之后有很大的改动:
3.0之前:

private Request networkRequest(Request request) throws IOException {
  Request.Builder result = request.newBuilder();

  //例行省略....

  CookieHandler cookieHandler = client.getCookieHandler();
  if (cookieHandler != null) {
    // Capture the request headers added so far so that they can be offered to the CookieHandler.
    // This is mostly to stay close to the RI; it is unlikely any of the headers above would
    // affect cookie choice besides "Host".
    Map<String, List<String>> headers = OkHeaders.toMultimap(result.build().headers(), null);

    Map<String, List<String>> cookies = cookieHandler.get(request.uri(), headers);

    // Add any new cookies to the request.
    OkHeaders.addCookies(result, cookies);
  }

  //例行省略....

  return result.build();
}

我们可以看到Request中是有CookieHandler的,通过client.getCookieHandler()函数获得了CookieHandler对象,通过该对象拿到cookie并设置到请求头里,请求结束后取得响应后通过networkResponse.headers()函数将请求头获得传入receiveHeaders函数,并将取得的cookie存入getCookieHandler得到的一个CookieHandler对象中去。那么我们可以这样加:

OkHttpClient client = new OkHttpClient();
client.setCookieHandler(CookieHandler cookieHanlder);

3.0之后:
3.0之后OKHttp是加了CookieJar和Cookie两个类的

private Request networkRequest(Request request) throws IOException {
    Request.Builder result = request.newBuilder();

    //例行省略....

    List<Cookie> cookies = client.cookieJar().loadForRequest(request.url());
    if (!cookies.isEmpty()) {
      result.header("Cookie", cookieHeader(cookies));
    }

    //例行省略....

    return result.build();
  }
private String cookieHeader(List<Cookie> cookies) {
    StringBuilder cookieHeader = new StringBuilder();
    for (int i = 0, size = cookies.size(); i < size; i++) {
      if (i > 0) {
        cookieHeader.append("; ");
      }
      Cookie cookie = cookies.get(i);
      cookieHeader.append(cookie.name()).append('=').append(cookie.value());
    }
    return cookieHeader.toString();
  }
public void receiveHeaders(Headers headers) throws IOException {
    if (client.cookieJar() == CookieJar.NO_COOKIES) return;

    List<Cookie> cookies = Cookie.parseAll(userRequest.url(), headers);
    if (cookies.isEmpty()) return;

    client.cookieJar().saveFromResponse(userRequest.url(), cookies);
  }

那么我们该怎么持久化管理呢?重点来了:

OkHttpClient client = new OkHttpClient.Builder()
    .cookieJar(new CookieJar() {
        private final HashMap<HttpUrl, List<Cookie>> cookieStore = new HashMap<>();

        @Override
        public void saveFromResponse(HttpUrl url, List<Cookie> cookies) {
            cookieStore.put(url, cookies);
        }

        @Override
        public List<Cookie> loadForRequest(HttpUrl url) {
            List<Cookie> cookies = cookieStore.get(url);
            return cookies != null ? cookies : new ArrayList<Cookie>();
        }
    })
    .build();

上面的.addCookieJar就成功的帮我们将session和cookie放到了请求头里面,当调用了用户登录的接口之后,服务器接口的header里面的cookie和session我们就已经保存了。同时问题也来了,我们可以看出来并没有将cookie存到本地,也就是说当我们将APP关闭之后,如果你不再次调用登录接口就去直接调用别的接口,用户的cookie是错误的,服务器不识别你的当前用户,当然最笨的方法就是每次进入APP的时候都调用一下登录接口,这样也可以实现,但是….
所以我的解决办法:

这里写图片描述
重点就在这:
JavaNetCookieJar是CookieHandler的一个代理,它实现了CookieJar,我们可以看到它里面的loadForRequest()方法帮助我们从请求头里面获取了cookie并且通过saveFromResponse()保存了下来。

@Override public List<Cookie> loadForRequest(HttpUrl url) {
    // The RI passes all headers. We don't have 'em, so we don't pass 'em!
    Map<String, List<String>> headers = Collections.emptyMap();
    Map<String, List<String>> cookieHeaders;
    try {
      cookieHeaders = cookieHandler.get(url.uri(), headers);
    } catch (IOException e) {
      Internal.logger.log(WARNING, "Loading cookies failed for " + url.resolve("/..."), e);
      return Collections.emptyList();
    }

    List<Cookie> cookies = null;
    for (Map.Entry<String, List<String>> entry : cookieHeaders.entrySet()) {
      String key = entry.getKey();
      if (("Cookie".equalsIgnoreCase(key) || "Cookie2".equalsIgnoreCase(key))
          && !entry.getValue().isEmpty()) {
        for (String header : entry.getValue()) {
          if (cookies == null) cookies = new ArrayList<>();
          cookies.addAll(decodeHeaderAsJavaNetCookies(url, header));
        }
      }
    }

    return cookies != null
        ? Collections.unmodifiableList(cookies)
        : Collections.<Cookie>emptyList();
  }
@Override public void saveFromResponse(HttpUrl url, List<Cookie> cookies) {
    if (cookieHandler != null) {
      List<String> cookieStrings = new ArrayList<>();
      for (Cookie cookie : cookies) {
        cookieStrings.add(cookie.toString());
      }
      Map<String, List<String>> multimap = Collections.singletonMap("Set-Cookie", cookieStrings);
      try {
        cookieHandler.put(url.uri(), multimap);
      } catch (IOException e) {
        Internal.logger.log(WARNING, "Saving cookies failed for " + url.resolve("/..."), e);
      }
    }
  }

可以还是并没有实现本地保存啊,接着往下看:

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;
import android.util.Log;

import java.io.*;
import java.net.CookieStore;
import java.net.HttpCookie;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
/**
 * Created by <a href="http://www.jiechic.com" target="_blank">jiechic</a> on 15/5/27.
 */
/**
 * A persistent cookie store which implements the Apache HttpClient CookieStore interface.
 * Cookies are stored and will persist on the user's device between application sessions since they
 * are serialized and stored in SharedPreferences. Instances of this class are
 * designed to be used with AsyncHttpClient#setCookieStore, but can also be used with a
 * regular old apache HttpClient/HttpContext if you prefer.
 */
public class PersistentCookieStore implements CookieStore {

    private static final String LOG_TAG = "PersistentCookieStore";
    private static final String COOKIE_PREFS = "CookiePrefsFile";
    private static final String COOKIE_NAME_PREFIX = "cookie_";

    private final HashMap<String, ConcurrentHashMap<String, HttpCookie>> 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 HashMap<String, ConcurrentHashMap<String, HttpCookie>>();

        // Load any previously stored cookies into the store
        Map<String, ?> prefsMap = cookiePrefs.getAll();
        for(Map.Entry<String, ?> entry : prefsMap.entrySet()) {
            if (((String)entry.getValue()) != null && !((String)entry.getValue()).startsWith(COOKIE_NAME_PREFIX)) {
                String[] cookieNames = TextUtils.split((String)entry.getValue(), ",");
                for (String name : cookieNames) {
                    String encodedCookie = cookiePrefs.getString(COOKIE_NAME_PREFIX + name, null);
                    if (encodedCookie != null) {
                        HttpCookie decodedCookie = decodeCookie(encodedCookie);
                        if (decodedCookie != null) {
                            if(!cookies.containsKey(entry.getKey()))
                                cookies.put(entry.getKey(), new ConcurrentHashMap<String, HttpCookie>());
                            cookies.get(entry.getKey()).put(name, decodedCookie);
                        }
                    }
                }

            }
        }
    }

    @Override
    public void add(URI uri, HttpCookie cookie) {

        // Save cookie into local store, or remove if expired
        if (!cookie.hasExpired()) {
            if(!cookies.containsKey(cookie.getDomain()))
                cookies.put(cookie.getDomain(), new ConcurrentHashMap<String, HttpCookie>());
            cookies.get(cookie.getDomain()).put(cookie.getName(), cookie);
        } else {
            if(cookies.containsKey(cookie.getDomain()))
                cookies.get(cookie.getDomain()).remove(cookie.getDomain());
        }

        // Save cookie into persistent store
        SharedPreferences.Editor prefsWriter = cookiePrefs.edit();
        prefsWriter.putString(cookie.getDomain(), TextUtils.join(",", cookies.get(cookie.getDomain()).keySet()));
        prefsWriter.putString(COOKIE_NAME_PREFIX + cookie.getName(), encodeCookie(new SerializableHttpCookie(cookie)));
        prefsWriter.commit();
    }

    protected String getCookieToken(URI uri, HttpCookie cookie) {
        return cookie.getName() + cookie.getDomain();
    }

    @Override
    public List<HttpCookie> get(URI uri) {
        ArrayList<HttpCookie> ret = new ArrayList<HttpCookie>();
        for (String key:cookies.keySet()){
            if (uri.getHost().contains(key)){
                ret.addAll(cookies.get(key).values());
            }
        }
        return ret;
    }

    @Override
    public boolean removeAll() {
        SharedPreferences.Editor prefsWriter = cookiePrefs.edit();
        prefsWriter.clear();
        prefsWriter.commit();
        cookies.clear();
        return true;
    }


    @Override
    public boolean remove(URI uri, HttpCookie cookie) {
        String name = getCookieToken(uri, cookie);

        if(cookies.containsKey(uri.getHost()) && cookies.get(uri.getHost()).containsKey(name)) {
            cookies.get(uri.getHost()).remove(name);

            SharedPreferences.Editor prefsWriter = cookiePrefs.edit();
            if(cookiePrefs.contains(COOKIE_NAME_PREFIX + name)) {
                prefsWriter.remove(COOKIE_NAME_PREFIX + name);
            }
            prefsWriter.putString(uri.getHost(), TextUtils.join(",", cookies.get(uri.getHost()).keySet()));
            prefsWriter.commit();

            return true;
        } else {
            return false;
        }
    }

    @Override
    public List<HttpCookie> getCookies() {
        ArrayList<HttpCookie> ret = new ArrayList<HttpCookie>();
        for (String key : cookies.keySet())
            ret.addAll(cookies.get(key).values());

        return ret;
    }

    @Override
    public List<URI> getURIs() {
        ArrayList<URI> ret = new ArrayList<URI>();
        for (String key : cookies.keySet())
            try {
                ret.add(new URI(key));
            } catch (URISyntaxException e) {
                e.printStackTrace();
            }

        return ret;
    }

    /**
     * Serializes Cookie object into String
     *
     * @param cookie cookie to be encoded, can be null
     * @return cookie encoded as String
     */
    protected String encodeCookie(SerializableHttpCookie cookie) {
        if (cookie == null)
            return null;
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        try {
            ObjectOutputStream outputStream = new ObjectOutputStream(os);
            outputStream.writeObject(cookie);
        } catch (IOException e) {
            Log.d(LOG_TAG, "IOException in encodeCookie", 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 HttpCookie decodeCookie(String cookieString) {
        byte[] bytes = hexStringToByteArray(cookieString);
        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
        HttpCookie cookie = null;
        try {
            ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream);
            cookie = ((SerializableHttpCookie) objectInputStream.readObject()).getCookie();
        } catch (IOException e) {
            Log.d(LOG_TAG, "IOException in decodeCookie", e);
        } catch (ClassNotFoundException e) {
            Log.d(LOG_TAG, "ClassNotFoundException in decodeCookie", e);
        }

        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;
    }
}

我们自定义一个CookieStore来本地存储cookie,存储到SharedPreferences里。当然你也可以用这个:
https://github.com/franmontiel/PersistentCookieJar
都是可以的,这样就完美实现了cookie的持久化管理。
下面是小编封装的一个网络请求类,不喜勿喷,哈哈:


import android.content.Context;

import com.hankkin.bpm.Constant;
import com.hankkin.bpm.http.cookie.PersistentCookieStore;

import java.net.CookieHandler;
import java.net.CookieManager;
import java.net.CookiePolicy;
import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import okhttp3.internal.JavaNetCookieJar;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by Hankkin on 16/4/25.
 */
public class HttpControl {

    private static HttpControl mInstance;
    private Retrofit retrofit;

    public static HttpControl getInstance(Context context){
        if (mInstance == null){
            synchronized (HttpControl.class){
                if (mInstance == null)
                    mInstance = new HttpControl(context);
            }
        }
        return mInstance;
    }
    public HttpControl(Context context){

        CookieHandler cookieHandler = new CookieManager(new PersistentCookieStore(context),
                CookiePolicy.ACCEPT_ALL);


        HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
        logging.setLevel(HttpLoggingInterceptor.Level.BODY);



        OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .readTimeout(7676, TimeUnit.MILLISECONDS)
                .connectTimeout(7676,TimeUnit.MILLISECONDS)
                .addInterceptor(logging)
                .cookieJar(new JavaNetCookieJar(cookieHandler))
                .build();


        retrofit = new Retrofit.Builder().
                baseUrl(Constant.BASE_URL).
                addConverterFactory(GsonConverterFactory.create())
                .client(okHttpClient)
                .build();
    }



    public  <T> T createService(Class<T> clz){
        return retrofit.create(clz);
    }
}

补充:在这里可以查到SerializableHttpCookie这个文件
http://www.2cto.com/kf/201507/419264.html
感谢大家的提醒谢谢啦

推荐几篇这方面的资料,小编也是参考了一下资料:
http://stackoverflow.com/questions/34881775/automatic-cookie-handling-with-okhttp-3
http://www.open-open.com/lib/view/open1453422314105.html
http://stackoverflow.com/questions/tagged/okhttp?sort=active

版权声明:本文为博主原创文章,转载请标明出处。

相关文章推荐

Retrofit在客户端保持Cookie(服务器的Token验证)

移动端项目和网站还是多多少少的区别的,拿这个用户登陆过期验证这个流程来说吧,一般的网站是不会做处理的,他们访问的时候会自动验证Cookie的有效期的,移动端这样处理就不行的,他是不会保存Cookie的...

Android OkHttp的Cookie自动化管理

Android中在使用OkHttp这个库的时候,有时候需要持久化Cookie,那么怎么实现呢。OkHttp的内部源码过于复杂,不进行深究,这里只看其中的HttpEngineer里面的部分源码,在发起请...

Android 识别二代身份证证件信息与Camera取景

识别身份证信息需要用到图像处理的知识,无奈水平不够,于是寻找第三方接口,但是找到的都是收费的,后来找到一个叫云脉的产品http://ocr.ccyunmai.com/,发现他可以免费使用15天,但是1...

Android 分享一个SharedPreferences的工具类,方便保存数据

我们平常保存一些数据,都会用到SharedPreferences,他是保存在手机里面的,具体路径是data/data/你的包名/shared_prefs/保存的文件名.xml, SharedPrefe...

OkHttp与Cookie及Cookie的持久化

一、OkHttp3下的Cookie的使用 ①、OkHttpClient取消了setCookieHandler(CookieHandler cookieHandler); 改而使用: setCookie...

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

首先cookie是什么就不多说了,还是不知道的话推荐看看这篇文章 Cookie/Session机制详解 深入解析Cookie技术为什么要持久化cookie也不多说了,你能看到这篇文章代表你有这个需...

开源项目OkHttpPlus——支持GET、POST、UI线程回调、JSON格式解析、链式调用、文件上传下载

开源项目OkHttpPlus——OkHttp封装库,支持GET、POST、UI线程回调、JSON格式解析、链式调用、小文件上传下载及进度监听OkHttpPlus介绍项目地址:https://githu...

okhttp3 cookie持久化

PersistentCookieStore.java package com.qlzgzg.nettrade.android.cookie; import android.content.Cont...

Android基于Retrofit2.0 封装的超好用的RetrofitClient工具类(完美结合RxJava)

原文地址 :http://www.jianshu.com/p/29c2a9ac5abf RetrofitClient 基于Retrofit2.0封装的RetrofitClient. 避免重复...

Android 网络框架 Retrofit2.0介绍、使用和封装

前言 时至今日,Android的网络框架不再像之前那么到处都是,随着Google把 HttpClient直接删掉,似乎意味着Android越来越成熟。网络框架中的佼佼者Volley也不再那么光鲜,取...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)