El Lord Code El Lord Code - 1 year ago 146
Java Question

Non proxy hosts for OkHttp3

How can I set what hostnames should not pass in one request made by

using a proxy?

Is there anything equivalent for the vm argument
in OkHttp3?

For example:

final OkHttpClient okHttpClient = new OkHttpClient
.connectTimeout(60, TimeUnit.SECONDS)
.writeTimeout(60, TimeUnit.SECONDS)
.readTimeout(60, TimeUnit.SECONDS)
.proxy(new Proxy(Proxy.Type.HTTP,
new InetSocketAddress(defaultProxyHostName, Integer.parseInt(defaultProxyPort))))

Answer Source

After digging around a bit, I have found out how to do it.

So I had to create a ProxySelector implementing the logic when the proxy should be applied or not.


final ProxySelector proxySelector = new ProxySelector() {
    public java.util.List<Proxy> select(final URI uri) {
        final List<Proxy> proxyList = new ArrayList<Proxy>(1);

        // Host
        final String host = uri.getHost();

        // Is an internal host
        if (host.startsWith("") || StringUtils.contains(nonProxyHostsValue, host)) {
        } else {
            // Add proxy
            proxyList.add(new Proxy(Type.HTTP,
                    new InetSocketAddress(proxyHostNameValue, Integer.parseInt(proxyPortValue))));

        return proxyList;

    public void connectFailed(URI arg0, SocketAddress arg1, IOException arg2) {
        throw new UnsupportedOperationException("Not supported yet.");

// Set proxy selector