D
D
Drunk_bear642019-06-12 22:10:09
Java
Drunk_bear64, 2019-06-12 22:10:09

How to get Request Cookie on Retrofit?

Hello, I am writing an application on Android Studio. There is a server, there are requests that I send from the client through Retrofit. How can I get a Request Cookie?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Peter, 2019-06-13
@Drunk_bear64

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()) {
            // ...
         }
        return originalResponse;
    }
}

OkHttpClient.Builder okHttpClient = new OkHttpClient().newBuilder();
okHttpClient.interceptors().add(new ReceivedCookiesInterceptor());
            
retrofit = new Retrofit.Builder()
                 .baseUrl(BASE_URL)
                 .client(okHttpClient.build())
                 .addConverterFactory(GsonConverterFactory.create())
                 .build();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question