How to get OkHttp3 redirected URL?

The response object provides a chain of the requests and responses which were used to obtain it.

To obtain the final URL, call request() on the Response for the final Request which then provides the url() you desire.

You can follow the entire response chain by calling priorResponse() and looking at each Response's associated Request.


The OkHttp.Builder has NetworkInterceptor provided.Here is an example:

        OkHttpClient httpClient = new OkHttpClient.Builder()
            .addNetworkInterceptor(new Interceptor() {
                @Override
                public Response intercept(Chain chain) throws IOException {
                    System.out.println("url: " + chain.request().url());
                    return chain.proceed(chain.request());
                }
            })
            .build();
    System.out.println(httpClient.newCall(new Request.Builder().url("http://google.com").build()).execute());

OkHttp3 wiki: Interceptors