OKHttp实现Cookie持久化

    xiaoxiao2021-03-26  29

    直接上代码 PersistentCookieStore 类

    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, 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 HashMap<String, ConcurrentHashMap<String, Cookie>>(); // 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) { Cookie decodedCookie = decodeCookie(encodedCookie); if (decodedCookie != null) { if (!cookies.containsKey(entry.getKey())) cookies.put(entry.getKey(), new ConcurrentHashMap<String, Cookie>()); cookies.get(entry.getKey()).put(name, decodedCookie); } } } } } } protected void add(HttpUrl uri, Cookie cookie) { String name = getCookieToken(cookie); if (!cookie.persistent()) { if (!cookies.containsKey(uri.host())) { cookies.put(uri.host(), new ConcurrentHashMap<String, Cookie>()); } cookies.get(uri.host()).put(name, cookie); } else { if (cookies.containsKey(uri.host())) { cookies.get(uri.host()).remove(name); }else { return ; } } // Save cookie into persistent store SharedPreferences.Editor prefsWriter = cookiePrefs.edit(); prefsWriter.putString(uri.host(), TextUtils.join(",", cookies.get(uri.host()).keySet())); prefsWriter.putString(COOKIE_NAME_PREFIX + name, encodeCookie(new SerializableHttpCookie(cookie))); prefsWriter.apply(); } protected String getCookieToken(Cookie cookie) { return cookie.name() + cookie.domain(); } @Override public void add(HttpUrl uri, List<Cookie> cookies) { for (Cookie cookie : cookies) { add(uri, cookie); } } @Override public List<Cookie> get(HttpUrl uri) { ArrayList<Cookie> ret = new ArrayList<Cookie>(); if (cookies.containsKey(uri.host())) { Collection<Cookie> cookies = this.cookies.get(uri.host()).values(); for (Cookie cookie : cookies) { if (isCookieExpired(cookie)) { remove(uri, cookie); } else { ret.add(cookie); } } } return ret; } private static boolean isCookieExpired(Cookie cookie) { return cookie.expiresAt() < System.currentTimeMillis(); } @Override public boolean removeAll() { SharedPreferences.Editor prefsWriter = cookiePrefs.edit(); prefsWriter.clear(); prefsWriter.apply(); cookies.clear(); return true; } @Override public boolean remove(HttpUrl uri, Cookie cookie) { String name = getCookieToken(cookie); if (cookies.containsKey(uri.host()) && cookies.get(uri.host()).containsKey(name)) { cookies.get(uri.host()).remove(name); SharedPreferences.Editor prefsWriter = cookiePrefs.edit(); if (cookiePrefs.contains(COOKIE_NAME_PREFIX + name)) { prefsWriter.remove(COOKIE_NAME_PREFIX + name); } prefsWriter.putString(uri.host(), TextUtils.join(",", cookies.get(uri.host()).keySet())); prefsWriter.apply(); return true; } else { return false; } } @Override public List<Cookie> getCookies() { ArrayList<Cookie> ret = new ArrayList<Cookie>(); for (String key : cookies.keySet()) ret.addAll(cookies.get(key).values()); return ret; } 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()); } protected Cookie decodeCookie(String cookieString) { byte[] bytes = hexStringToByteArray(cookieString); ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes); Cookie 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; } }

    SerializableHttpCookie类

    public class SerializableHttpCookie implements Serializable{ private static final long serialVersionUID = 6374381323722046732L; private transient final Cookie cookie; private transient Cookie clientCookie; public SerializableHttpCookie(Cookie cookie) { this.cookie = cookie; } public Cookie getCookie() { Cookie bestCookie = cookie; if (clientCookie != null) { bestCookie = clientCookie; } return bestCookie; } private void writeObject(ObjectOutputStream out) throws IOException { out.writeObject(cookie.name()); out.writeObject(cookie.value()); out.writeLong(cookie.expiresAt()); out.writeObject(cookie.domain()); out.writeObject(cookie.path()); out.writeBoolean(cookie.secure()); out.writeBoolean(cookie.httpOnly()); out.writeBoolean(cookie.hostOnly()); out.writeBoolean(cookie.persistent()); } private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException { String name = (String) in.readObject(); String value = (String) in.readObject(); long expiresAt = in.readLong(); String domain = (String) in.readObject(); String path = (String) in.readObject(); boolean secure = in.readBoolean(); boolean httpOnly = in.readBoolean(); boolean hostOnly = in.readBoolean(); boolean persistent = in.readBoolean(); Cookie.Builder builder = new Cookie.Builder(); builder = builder.name(name); builder = builder.value(value); builder = builder.expiresAt(expiresAt); builder = hostOnly ? builder.hostOnlyDomain(domain) : builder.domain(domain); builder = builder.path(path); builder = secure ? builder.secure() : builder; builder = httpOnly ? builder.httpOnly() : builder; clientCookie = builder.build(); } }

    CookieStore接口

    public interface CookieStore { void add(HttpUrl uri, List<Cookie> cookie); List<Cookie> get(HttpUrl uri); List<Cookie> getCookies(); boolean remove(HttpUrl uri, Cookie cookie); boolean removeAll(); }

    CookieJarImpl类 public class CookieJarImpl implements CookieJar{ private CookieStore cookieStore;

    public CookieJarImpl(CookieStore cookieStore) { if (cookieStore == null) Exceptions.illegalArgument("cookieStore can not be null."); this.cookieStore = cookieStore; } @Override public synchronized void saveFromResponse(HttpUrl url, List<Cookie> cookies) { cookieStore.add(url, cookies); } @Override public synchronized List<Cookie> loadForRequest(HttpUrl url) { return cookieStore.get(url); } public CookieStore getCookieStore() { return cookieStore; }

    } Exceptions类

    public class Exceptions { public static void illegalArgument(String msg, Object... params) { throw new IllegalArgumentException(String.format(msg, params)); } }

    使用: 在Applicaion中对OkHttp进行初始化设置

    private static OkHttpClient okHttpClient; @Override public void onCreate() { super.onCreate(); Log.e("DEBUG","Application"); CookieJarImpl cookieJar = new CookieJarImpl(new PersistentCookieStore(getApplicationContext())); okHttpClient = new OkHttpClient.Builder() .connectTimeout(10000L, TimeUnit.MILLISECONDS) .readTimeout(10000L, TimeUnit.MILLISECONDS) .cookieJar(cookieJar) //其他配置 .build(); } //通过该方法将okHttpClient返回 public static OkHttpClient getOkHttpClient(){ return okHttpClient; } }
    转载请注明原文地址: https://ju.6miu.com/read-662282.html

    最新回复(0)