java 使用Retrofit / OkHttp轻松处理Cookies

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 使用Retrofit / OkHttp轻松处理Cookies相关的知识,希望对你有一定的参考价值。

/**
 * 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<>());
        }
    }
}

以上是关于java 使用Retrofit / OkHttp轻松处理Cookies的主要内容,如果未能解决你的问题,请参考以下文章

java 用于OkHttp和Retrofit的GZip日志拦截器

Retrofit--使用Retrofit时怎样去设置OKHttp

Retrofit+Okhttp 在 Android 中是不是默认使用 httpCaching?

「2020 新手必备 」极速入门 Retrofit + OkHttp 网络框架到实战,这一篇就够了!

带你走通 OkHttp+Retrofit+Rxjava

带你走通 OkHttp+Retrofit+Rxjava