Skip to content

Instantly share code, notes, and snippets.

@wangsha
Last active January 23, 2016 05:14
Show Gist options
  • Save wangsha/4c4e2b9141060fb56b81 to your computer and use it in GitHub Desktop.
Save wangsha/4c4e2b9141060fb56b81 to your computer and use it in GitHub Desktop.
import java.io.IOException;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;
/**
* Created by sha on 23/1/16.
*/
public class CookieRedirectInterceptor implements Interceptor {
String cookies = "";
String redirectURL = "";
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
Response response = null;
if (request.url().toString().equals(redirectURL)) {
Request requestWithCookies = request.newBuilder()
.addHeader("Cookie", cookies).build();
response = chain.proceed(requestWithCookies);
} else {
response = chain.proceed(request);
if (response.isRedirect()) {
this.redirectURL = response.header("Location", "");
this.cookies = response.header("Set-Cookie", "");
}
}
return response;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment