Skip to content

Instantly share code, notes, and snippets.

@twiceyuan
Forked from tsuharesu/AddCookiesInterceptor.java
Last active June 25, 2019 02:36
Show Gist options
  • Save twiceyuan/909331b966a63cf9538985e697e2c148 to your computer and use it in GitHub Desktop.
Save twiceyuan/909331b966a63cf9538985e697e2c148 to your computer and use it in GitHub Desktop.
[OkHttp Cookie 处理] Handle Cookies easily with Retrofit/OkHttp #Android
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.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.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<>());
}
}
}
/**
* 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());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment