10

I use 100% working socks and I can't connect through my application.

        SocketAddress proxyAddr = new InetSocketAddress("1.1.1.1", 12345);
        Proxy pr = new Proxy(Proxy.Type.SOCKS, proxyAddr);

    try
    {
        HttpURLConnection con = (HttpURLConnection) new URL(url).openConnection(pr);
        con.setConnectTimeout(proxyTimeout * 1000);
        con.setReadTimeout(proxyTimeout * 1000);
        con.connect();

        System.out.println(con.usingProxy());
    }
    catch(IOException ex)
    {
        Logger.getLogger(Enter.class.getName()).log(Level.SEVERE, null, ex);
    }

So what am I doing wrong? If I'll use HTTP with some HTTP proxy all is working but not with SOCKS.

CloudyMarble
  • 36,908
  • 70
  • 97
  • 130
Clark
  • 2,083
  • 6
  • 32
  • 47
  • 2
    What error message do you get? *How* does this code fail to work? Also, are you using a SOCKS4 or SOCKS5 proxy – Daniel Martin Apr 18 '11 at 00:32
  • I tried to use both of SOCKS4 and SOCKS5. This is the error message: http://pastebin.com/xFB2qZQs But! Socks is 100% woking (socks5 here is). – Clark Apr 18 '11 at 13:03
  • Where i can get SOCKS5 library for android?? i want to use it also.. – Noman May 30 '13 at 10:19

4 Answers4

26

It's really easy. You just need to set the relevant System Properties and just get on with your regular HttpConnection.

System.getProperties().put( "proxySet", "true" );
System.getProperties().put( "socksProxyHost", "127.0.0.1" );
System.getProperties().put( "socksProxyPort", "1234" );
2hamed
  • 8,719
  • 13
  • 69
  • 112
  • 1
    These are correct but what to do in case I need to use multi threads and I need to have many Proxies per time ? For example I found socks proxy which works fine in IE as SOCKS (and does not wotk as HTTP and other protocols) but when I use it in Java this does not work for v4 and for 5 ! That seems like hug suck in Java – Andrew Niken Nov 23 '16 at 02:29
  • It's worth to mention that Java will ignore the SOCKS5 settings unless proxyset is set. This is not necessary for using an HTTP proxy. – Raúl Salinas-Monteagudo May 06 '19 at 13:40
7

Inform the "socksProxyHost" and "socksProxyPort" VM arguments.

e.g.

java -DsocksProxyHost=127.0.0.1 -DsocksProxyPort=8080 org.example.Main
Marcelo C.
  • 3,822
  • 2
  • 22
  • 11
2

http://grepcode.com/file_/repository.grepcode.com/java/root/jdk/openjdk/8u40-b25/sun/net/www/http/HttpClient.java/?v=source

Deep down, HttpClient is used in HttpURLConnection.

if ((proxy != null) && (proxy.type() == Proxy.Type.HTTP)) {
   sun.net.www.URLConnection.setProxiedHost(host);
   privilegedOpenServer((InetSocketAddress) proxy.address());
   usingProxy = true;
   return;
} else {
   // make direct connection
   openServer(host, port);
   usingProxy = false;
   return;
}

On line 476, you can see that the only acceptable proxy is an HTTP proxy. It makes a direct connection otherwise.

Weirdly there's is no support for SOCKS proxy using HttpURLConnection. Even worse, the code doesn't even t using an unsupported proxy and just ignores the proxy!

Why there isn't any support for SOCKS proxies after at least 10 years of this class' existence cannot be explained.

Stephan
  • 41,764
  • 65
  • 238
  • 329
1

Or this answer: https://stackoverflow.com/a/64649010/5352325

If you know which URIs need to go to proxy, you can also use the low layer ProxySelector: https://docs.oracle.com/javase/7/docs/technotes/guides/net/proxies.html where for each Socket connection that is made, you can decide what proxies are to be used.

It looks something like this:

public class MyProxySelector extends ProxySelector {
        ...

        public java.util.List<Proxy> select(URI uri) {
        ...
          if (uri is what I need) {
             return list of my Proxies
          }
        ...
        }
        ...
}
 

Then you make use of your selector:

public static void main(String[] args) {
        MyProxySelector ps = new MyProxySelector(ProxySelector.getDefault());
        ProxySelector.setDefault(ps);
        // rest of the application
}
Pavol Zibrita
  • 4,222
  • 1
  • 10
  • 6