Retrofit2添加Cookie

(转载)http://tsuharesu.com/handling-cookies-with-okhttp/

When you are dealing with APIs you always have to save your user tokens somewhere. What most developers do (I tend to think) is to use Android SharedPreferences as it is easy to save and retrieve things. (You could use a Database too, it's your choice)

But how to get these Cookies when they are given to us on a response? And how to send them back on the next requests? OkHttp don't handle Cookies transparently, like say, Ion. But it's "easy" to do it yourself.

What I do is to intercept my Responses via Interceptors (OkHttp 2.2+) and use the same OkClient on all next requests. Every time a new Cookie is received, it is saved on my SharedPreferences and on every Request done after that, the Cookie is added "magically".

This is a simple solution, maybe there are betters on the wild. But I couldn't find anything as easy as this for a long time. Hope you enjoy.

The gist: https://gist.github.com/cbfd8f02d46498b01f1b

  /**
  * This interceptor put all the Cookies in Preferences in the Request.
  * Your implementation on how to get the Preferences MAY VARY.
  * <p>
  * Created by tsuharesu on 4/1/15.
  */
  public class AddCookiesInterceptor implements Interceptor {
   
  @Override
  public Response intercept(Chain chain) throws IOException {
  Request.Builder builder = chain.request().newBuilder();
  HashSet<String> preferences = (HashSet) Preferences.getDefaultPreferences().getStringSet(Preferences.PREF_COOKIES, new HashSet<>());
  for (String cookie : preferences) {
  builder.addHeader("Cookie", cookie);
  Log.v("OkHttp", "Adding Header: " + cookie); // This is done so I know which headers are being added; this interceptor is used after the normal logging of OkHttp
  }
   
  return chain.proceed(builder.build());
  }
  }
  /**
  * This Interceptor add all received Cookies to the app DefaultPreferences.
  * Your implementation on how to save the Cookies on the Preferences MAY VARY.
  * <p>
  * Created by tsuharesu on 4/1/15.
  */
  public class ReceivedCookiesInterceptor implements Interceptor {
  @Override
  public Response intercept(Chain chain) throws IOException {
  Response originalResponse = chain.proceed(chain.request());
   
  if (!originalResponse.headers("Set-Cookie").isEmpty()) {
  HashSet<String> cookies = new HashSet<>();
   
  for (String header : originalResponse.headers("Set-Cookie")) {
  cookies.add(header);
  }
   
  Preferences.getDefaultPreferences().edit()
  .putStringSet(Preferences.PREF_COOKIES, cookies)
  .apply();
  }
   
  return originalResponse;
  }
  }
  /**
  * 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());
view raw RestClient.java hosted with ❤ by  GitHub
Tsuharesu

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值