How to use SOCKS in Java?

I use 100% work socks and I cannot 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); } 

code> So what am I doing wrong? If I use HTTP with some HTTP proxy, everything works, but not with SOCKS.

+6
source share
3 answers

It is very simple. You just need to set the appropriate system properties and just continue your regular HttpConnection.

 System.getProperties().put( "proxySet", "true" ); System.getProperties().put( "socksProxyHost", "127.0.0.1" ); System.getProperties().put( "socksProxyPort", "1234" ); 
+12
source

Report arguments to socksProxyHost and socksProxyPort VM.

eg.

 java -DsocksProxyHost=127.0.0.1 -DsocksProxyPort=8080 org.example.Main 
+2
source

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

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. This makes a direct connection.

There is hardly any support for a SOCKS proxy using HttpURLConnection. Even worse, the code does not even use an unsupported proxy server and simply ignores the proxy server!

Why is there no support for SOCKS proxies after at least 10 years of existence of this class can not be explained.

+1
source

Source: https://habr.com/ru/post/886108/


All Articles