7

How do I avoid going through the ProxySelector when making a connection with URLConnection or rather how to get a connection guaranteed free of whatever proxies Java knows about ?

I thought this was what Proxy.NO_PROXY was for. Quoting from the Javadoc:

A proxy setting that represents a DIRECT connection, basically telling the protocol handler not to use any proxying

Yet such a connection will still go through the ProxySelector. I don't get it ??

I've made a small test to prove my point:

public static void main(String[] args) throws MalformedURLException, IOException {
    ProxySelector.setDefault(new MyProxySelector());
    URL url = new URL("http://foobar.com/x1/x2");
    URLConnection connection = url.openConnection(Proxy.NO_PROXY);
    connection.connect();
}

and a dummy ProxySelector which does nothing but log what is going on:

public class MyProxySelector extends ProxySelector {

    @Override
    public List<Proxy> select(URI uri) {
        System.out.println("MyProxySelector called with URI = " + uri);
        return Collections.singletonList(Proxy.NO_PROXY);
    }

    @Override
    public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {}
}

which prints:

"MyProxySelector called with URI = socket://foobar.com:80"

(Note how the protocol has changed from http to socket)

I can of course create my ProxySelector in such a way so that it always returns Proxy.NO_PROXY if the URI scheme is socket but I guess there would be occasions where there's a SOCKS proxy on the site and then it wouldn't be true.

Let me restate the question: I need a way to make sure a specific URLConnection doesn't use a proxy, regardless of what System Properties may be set or what ProxySelector is installed.

peterh
  • 18,404
  • 12
  • 87
  • 115
  • 1
    I think you can also set `ProxySelector.setDefault(null);`. This will allow to direct connection. – Tunaki Nov 06 '15 at 21:23
  • @Tunaki. Unfortunately that will turn off the ProxySelector completely and for *all* connections. – peterh Nov 06 '15 at 21:40
  • 1
    The fact that direct connections go through the `ProxySelector` is an implementation detail. Why do you care? Do you have a *real* problem? – user207421 Nov 06 '15 at 22:28
  • @EJP. Yep. I need a way to make sure a specific network connection *isn't* proxied. – peterh Nov 06 '15 at 22:47
  • @EJP. I've added the very last sentence. I believe the requirement, to be able to have a proxy-less connection, is fair and justified. – peterh Nov 06 '15 at 23:19
  • It seems, when the URL handler recognizes that it should connect directly without a proxy, it creates a `Socket` without specifying a proxy but the `Socket` interprets an absent proxy as “use default proxy”. I’d consider this a bug and filing a bug report the best option. – Holger Nov 09 '15 at 11:55
  • @Holger. You are close. What happens when you request `NO_PROXY` on an URL Connection is that a socket of type `SocksSocketImpl` is created. This is just a normal SOCKS connection and will attempt to look up whatever SOCKS proxy is configured ... regardless of your request for `NO_PROXY`. I believe the main reason why this doesn't bother anyone and hasn't been reported as a bug is that fact that (1) very few people would ever attempt to do their own `ProxySelector` and more importantly: (2) very few sites have a SOCKS proxy installed. – peterh Nov 09 '15 at 17:29

1 Answers1

0

This is tracked as JDK bug 8144008.

peterh
  • 18,404
  • 12
  • 87
  • 115