Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get OkHttp3 redirected URL?

Is there a way to get the final URL of a request? I know that I can disable redirections and to this myself, but is there a way to get the current URL I'm loading? Like, if I requested a.com and got redirected to b.com, is there a way to get the name of the url b.com?

like image 326
Gatonito Avatar asked Aug 16 '16 03:08

Gatonito


People also ask

What is okhttp3 interceptor?

Interceptors, according to the documentation, are a powerful mechanism that can monitor, rewrite, and retry the API call. So, when we make an API call, we can either monitor it or perform some tasks.

Is OkHttp asynchronous?

OkHttp doesn't currently offer asynchronous APIs to receive a response body in parts.

What is Java Okhttpclient?

OkHttp is an efficient HTTP & HTTP/2 client for Android and Java applications. It comes with advanced features, such as connection pooling (if HTTP/2 isn't available), transparent GZIP compression, and response caching, to avoid the network completely for repeated requests.

What is interceptor chain?

The Chain object in retrofit is an implementation of the Chain of Responsibility design pattern, and each interceptor is a processing object which acquires the result of the previous interceptor through chain.


1 Answers

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.

like image 89
Jake Wharton Avatar answered Oct 14 '22 02:10

Jake Wharton