Prevent OkHttp / Retrofit from proxy bypass

When OkHttp tries a proxy, and this route fails once, every request after this request completely bypasses the Android proxy. So itโ€™s hard to debug Charlesโ€™s use.

I currently have the code below, but it is only configured to build Debug. It works great, but it's a hack, and we want to publish it for all users.

Is there a hidden OkHttpClient.dontBypassProxy setting that I cannot find? Or, does the code really look like a good solution?

OkHttpClient.Builder baseClientBuilder = new OkHttpClient.Builder().retryOnConnectionFailure(false).connectionPool(new ConnectionPool(5, 20, TimeUnit.SECONDS)); //Ask OkHttp what proxies we have setup List<Proxy> proxies = ProxySelector.getDefault().select(URI.create("http://www.somesite.com")); if (proxies.size() > 0 && proxies.get(0) != Proxy.NO_PROXY) { //Force OkHttp to always use this proxy baseClientBuilder.proxy(proxies.get(0)); } 
+7
android retrofit retrofit2
source share

All Articles