views:

52

answers:

3

I have a very simple code that uses HttpURLConnection to access some web site via proxy

    System.setProperty("java.net.useSystemProxies", "true");
    System.out.println("Proxy: " + ProxySelector.getDefault().select(new URI(urlS)));
    URL url = new URL(urlS);
    HttpURLConnection ic = (HttpURLConnection)url.openConnection();

    ic.connect();

For some reason, Java thinks that I need SOCKS proxy, not http, throwing the following exception:

ERROR: Can't connect to SOCKS proxy:Connection timed out: connect
A: 

Check that something has not set the "socksProxyHost" property in the Systems properties.

EDIT

The "useSystemProxies" property is described thus:

"On recent Windows systems and on Gnome 2.x platforms it is possible to tell the default ProxySelector to use the system proxy settings (both recent versions of Windows and Gnome 2.x let you set proxies globally through their user interface). If the system property java.net.useSystemProxies is set to true (by default it is set to false for compatibility sake), then the default ProxySelector will try to use these settings."

So, assuming that you have not supplied your own ProxySelector class, you should also check the system proxy settings to ensure that they don't say to use SOCKS.

Stephen C
No, its not set.
Demiurg
No, there is no SOCKS configuration in the system proxy settings. Moreover, I would expect that it would use HTTP proxy for HTTP protocol by default, or at least allow me to set this
Demiurg
I give up. Perhaps it is time to run your application using the Java debugger, set a breakpoint in the `DefaultProxySelector` and single step it.
Stephen C
Looks like a bug in JDK to me. Found some other [unresolved] reports of this problem
Demiurg
+1  A: 

You need to use the http.proxyHost system property instead. See http://java.sun.com/javase/6/docs/technotes/guides/net/proxies.html for details.

java -Dhttp.proxyHost=webcache.mydomain.com GetURL
Thorbjørn Ravn Andersen
What's wrong with "java.net.useSystemProxies" ? The article you referenced mentions it too.
Demiurg
Curiously enough, setting proxyHost and proxyPort does work. Even more curiously, ProxySelector.getDefault().select(new URI(urlS)) shows the same proxy configuration in both cases.
Demiurg
Is there a bug in java.net.useSystemProxies ?
Demiurg
I dó not know. Our primary production platform does not provide system proxy configuration.
Thorbjørn Ravn Andersen
A: 

If you are having this issues on Windows, you may run into a Java bug.

Java treats any system proxy setting as SOCKS. You have to either disable useSystemProxies or don't use proxy in Windows.

If proxy is needed, try to check "Use the same proxy server for all protocols". That fixed our problem.

ZZ Coder
This is what I thought. Some people suggested a workaround which involves overloading the DefaultProxySelector class which I'm going to try.
Demiurg