I have an authentication call that i'm trying to make using Retrofit on Android. The call returns a 302 to either a success or failure page. The original 302 response brings back a session cookie needed to maintain authentication on success, however Retrofit is automatically handing the request off to the redirect url before I get a chance to consume the cookie.
Is there a way to prevent following the redirect? Or is there a way to write a response handler on Retrofit that can add the appropriate header before making the second call?
to prevent the redirect you have to configure your client, e.g with OkHttp 2:
private sendRequest() {
OkHttpClient client = new OkHttpClient();
client.setFollowRedirects(false);
connectAdapter = new RestAdapter.Builder()
.setClient(new OkClient(client))
.setEndpoint("http://yourendpoint")
.setLogLevel(RestAdapter.LogLevel.FULL)
.build();
connectAdapter.create(YourRequest.class).sendMyRequest("login","password");
}
With OKHTTP 3 (you can read this answer from @gropapa):
OkHttpClient.Builder builder = new OkHttpClient.Builder();
builder.followRedirects(false);
OkHttpClient httpClient = builder.build();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With