How do I get a proxy free connection in Java?
When establishing a connection with urlconnection, how to avoid passing through proxyselector, or how to ensure that the connection is not affected by any agent known to Java?
I think this is proxy NO_ Purpose of proxy Reference Javadoc:
However, such a connection will still be through proxyselector Don't I understand?
I did a little 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 virtual proxyselector that does nothing but record what is happening:
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) {} }
Print:
"MyProxySelector called with URI = socket://foobar.com:80"
(note how the protocol changes from HTTP to socket)
Of course I can create my proxyselector so that it always returns proxy NO_ Proxy, if the URI scheme is socket, but I think it is possible to have a socks proxy on the website, and then it will not be true
Let me reiterate this problem: I need a way to ensure that a specific urlconnection does not use a proxy, no matter what system properties can be set or what proxyselector can be installed
Solution
This is traced to JDK bug 8144008