当 OkHttp 尝试代理,并且该路由失败一次时,该请求之后的每个请求都将完全绕过 Android 系统代理。所以很难用Charles来调试。
我目前有下面的代码,但它设置为仅用于调试版本。它工作得很好,但它是一个 hack,我们希望为所有用户发布它。
是否有一些我找不到的隐藏 OkHttpClient.dontBypassProxy 设置?或者,下面的代码看起来像一个好的解决方案吗?
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));
}
最佳答案
这已在 OkHttp 3.5.0 中修复
https://square.github.io/okhttp/changelog_3x/
OkHttp no longer attempts a direct connection if the system’s HTTP proxy fails. This behavior was surprising because OkHttp was disregarding the user’s specified configuration. If you need to customize proxy fallback behavior, implement your own java.net.ProxySelector.
关于android - 防止 OkHttp/Retrofit 绕过代理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36757076/