Java HttpURLConnection uses SOCKS proxy instead of HTTP

I have a very simple code that uses HttpURLConnection to access some website 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 I need a SOCKS proxy, not http, throwing the following exception:

 ERROR: Can't connect to SOCKS proxy:Connection timed out: connect 
+2
java windows proxy
source share
4 answers

If you are having problems on Windows, you may encounter a Java error.

Java treats any system proxy as SOCKS. You must either disable useSystemProxies or not use proxies in Windows.

If a proxy server is required, try unchecking "Use the same proxy server for all protocols", making sure that the SOCKS proxy server field is empty. This found a problem.

+5
source

The real problem is that Java assumes that the check "Use the same proxy server for all protocols" also affects the SOCKS proxy server (I don’t know the logic of this dialog on Windows, but it is at least confusing) If the check is installed, you activate proxies for both HTTP and SOCKS, which is unlikely to be the desired configuration. One way to fix it is to uncheck the box and leave the SOCKS field blank.

Finally, I decided to create a ProxySelector that first calls the default selector, and if it finds the same configuration for HTTP and SOCKS connections, it will skip the SOCKS proxy.

  public class SocksFixerProxySelector extends ProxySelector { ProxySelector base; public SocksFixerProxySelector() { base = ProxySelector.getDefault(); } @Override public List<Proxy> select(URI uri) { List<Proxy> baseList = base.select(uri); try { if (uri.getScheme().equals("socket")) { Proxy socksProxy = findByType(baseList, Type.SOCKS); if (socksProxy != null) { URI httpTestUri = new URI("http", uri.getHost(), uri.getPath(), uri.getFragment()); Proxy httpProxy = findByType(base.select(httpTestUri), Type.HTTP); if (httpProxy != null && socksProxy.address().equals(httpProxy.address())) { // Quitamos SOCKS List<Proxy> filteredList = new ArrayList<>(baseList); filteredList.remove(socksProxy); return filteredList; } } } } catch (Exception e) { } return baseList; } @Override public void connectFailed(URI uri, SocketAddress sa, IOException ioe) { base.connectFailed(uri, sa, ioe); } private Proxy findByType(List<Proxy> proxies, Proxy.Type type) { for (Proxy proxy : proxies) { if (proxy.type() == type) return proxy; } return null; } 

Perhaps the best solution would be to check the registry and determine the correct settings, but I did not want to mess with the Windows code (and all these script parameters also looked bad)

+2
source

Make sure that something does not set the "socksProxyHost" property in the system properties.

EDIT

The "useSystemProxies" property is described as follows:

"On recent Windows systems and on Gnome 2.x platforms, you can tell ProxySelector use the system’s proxy settings by default (both the latest versions of Windows and Gnome 2.x allow you to set proxies around the world through your user interface). If the java.net.useSystemProxies system property is set to true (the satellite is set to false by default), then by default ProxySelector will try to use these settings. "

So, if you have not provided your own ProxySelector class, you should also check the system proxy settings to make sure that they do not say that they use SOCKS.

+1
source

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

 java -Dhttp.proxyHost=webcache.mydomain.com GetURL 
+1
source

All Articles