Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to disable following redirects in OkHttp 2.0?

In Android, I'd like to use the new OkHttp 2.0 to request some URLs, but I'd like more control over redirects. I've already found the option to enable or disable following HTTPS → HTTP or HTTP → HTTPS redirects, but I'd like to not follow any redirects, so I can update my GUI as soon as possible, and choose whether to follow them from application logic. I don't see an option to do this. Is it possible, and if so, how can I achieve this?

like image 323
Dan Hulme Avatar asked Jun 02 '14 14:06

Dan Hulme


People also ask

What is the underlying library of OkHttp?

Beginning with Mobile SDK 4.2, the Android REST request system uses OkHttp (v3. 2.0), an open-source external library from Square Open Source, as its underlying architecture. This library replaces the Google Volley library from past releases.

What is OkHttp used for?

What is OkHttp? OkHttp is an HTTP client from Square for Java and Android applications. It's designed to load resources faster and save bandwidth. OkHttp is widely used in open-source projects and is the backbone of libraries like Retrofit, Picasso, and many others.


2 Answers

Yes it is possible in version 2.3.0

final OkHttpClient client = new OkHttpClient(); client.setFollowRedirects(false); 

For 3.x

OkHttpClient client = new OkHttpClient().newBuilder()                                             .followRedirects(false)                                             .followSslRedirects(false)                                             .build(); 
like image 66
Arik Avatar answered Nov 04 '22 02:11

Arik


Seems like it isn't possible, but it will supposedly make it into the next release, at least according to some of these comments on a related issue on Github.

Edit: It looks like it could be possible through OkUrlFactory, which is part of the okhttp-urlconnection submodule (still haven't figured out the actual purpose of this, but it looks like an Http(s)UrlConnection replacement).

Edit 2: Actually, hang on; it looks like it is implemented on master. However, it also looks like this didn't make it into the 2.0.0 release snapshot.

like image 34
kibibyte Avatar answered Nov 04 '22 03:11

kibibyte