twiceyuan
12/1/2016 - 5:08 AM

Handle Cookies easily with Retrofit/OkHttp

Handle Cookies easily with Retrofit/OkHttp

/**
 * 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());
import android.net.Uri;

import com.greenland.gclub.network.retrofit.CookieStore;

import java.io.IOException;
import java.util.HashSet;

import okhttp3.Interceptor;
import okhttp3.Response;

public class CookiesRecInterceptor implements Interceptor {

    @Override
    public Response intercept(Chain chain) throws IOException {
        String url = chain.request().url().toString();
        String host = Uri.parse(url).getHost();
        Response originalResponse = chain.proceed(chain.request());
        if (!originalResponse.headers("Set-Cookie").isEmpty()) {
            CookieStore.setCookies(host, new HashSet<>(originalResponse.headers("Set-Cookie")));
        }
        return originalResponse;
    }
}
import android.net.Uri;

import com.greenland.gclub.network.retrofit.CookieStore;

import java.io.IOException;
import java.util.Set;

import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

public class CookiesAddInterceptor implements Interceptor {

    @Override
    public Response intercept(Interceptor.Chain chain) throws IOException {

        String url = chain.request().url().toString();
        String host = Uri.parse(url).getHost();
        Request.Builder builder = chain.request().newBuilder();
        Set<String> preferences = CookieStore.getCookies(host);
        for (String cookie : preferences) {
            builder.addHeader("Cookie", cookie);
        }
        return chain.proceed(builder.build());
    }
}

import android.content.Context;
import android.content.SharedPreferences;

import com.example.app.App
import java.util.HashSet;
import java.util.Set;

/**
 * Created by twiceYuan on 01/12/2016.
 * Email: i@twiceyuan.com
 * Site: http://twiceyuan.com
 */

public class CookieStore {

    private static SharedPreferences sDefaultPreferences;

    private static SharedPreferences getDefaultPreferences() {
        if (sDefaultPreferences == null) {
            sDefaultPreferences = App.instance().getSharedPreferences("cookies_manager", Context.MODE_PRIVATE);
        }
        return sDefaultPreferences;
    }

    private static final Object LOCK = new Object();

    public static void setCookies(String host, Set<String> cookies) {
        synchronized (LOCK) {
            getDefaultPreferences().edit().putStringSet(host, cookies).apply();
        }
    }

    public static String getCookiesString(String host) {
        synchronized (LOCK) {
            Set<String> set = getDefaultPreferences().getStringSet(host, null);
            return set != null ? StringUtils.join(set, "; ") : "";
        }
    }

    public static Set<String> getCookies(String host) {
        synchronized (LOCK) {
            return getDefaultPreferences().getStringSet(host, new HashSet<>());
        }
    }
}