How to use HttpURLConnection through SOCKS proxies on Android

Well, I tried to use SOCKS proxy on Android. I struggle too much and I could not use it correctly.

Basically, I want to get the contents of the page through the SOCKS proxy using HttpURLConnection (this is required).

The code I use is:

  String proxyHost = "192.168.2.2"; int proxyPort = 1999; InetSocketAddress proxyAddr = new InetSocketAddress(proxyHost, proxyPort); Proxy proxy = new Proxy(Proxy.Type.SOCKS, proxyAddr); URL request = new URL("http://requestb.in/sgjga5sg"); HttpURLConnection urlConnection = (HttpURLConnection) request.openConnection(proxy); urlConnection.setDoInput(true); urlConnection.setConnectTimeout(36000); urlConnection.setReadTimeout(44000); InputStream is = urlConnection.getInputStream(); for (int x = is.read(); x >= 0; x = is.read()) { System.out.print((char) x); } System.out.println(); 

I get a SocketTimeOutException , here is the stack trace:

 java.net.SocketTimeoutException at java.net.PlainSocketImpl.read(PlainSocketImpl.java:488) at java.net.PlainSocketImpl.access$000(PlainSocketImpl.java:37) at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:237) at java.net.PlainSocketImpl.socksReadReply(PlainSocketImpl.java:440) at java.net.PlainSocketImpl.socksRequestConnection(PlainSocketImpl.java:340) at java.net.PlainSocketImpl.socksConnect(PlainSocketImpl.java:326) at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:181) at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:456) at java.net.Socket.connect(Socket.java:887) at com.android.okhttp.internal.Platform.connectSocket(Platform.java:174) at com.android.okhttp.Connection.connect(Connection.java:152) at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:276) at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:211) at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:382) at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:332) at com.android.okhttp.internal.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:199) at com.android.okhttp.internal.http.DelegatingHttpsURLConnection.getInputStream(DelegatingHttpsURLConnection.java:210) at com.android.okhttp.internal.http.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:25) at com.guness.testsocks.MainActivity.doTheThing(MainActivity.java:81) 

Some interesting things:

  • The proxy points to my SSH tunnel (apparently, it is being tested on other devices using the firefox proxy function).
  • When I change Proxy.Type to HTTP and select the appropriate HttpProxy from the Internet, it works, but I need socks.
  • Do not mind System.out.print and the loop, it is there for testing purposes.
  • Test device is model C6603 and version for Android 5.1.1

Edit: It appears that the SOCKS proxy does not work with HttpURLConnection on Android. Some links: This thread , and this thread claims. I want to be sure if there is a way without using third-party libraries.

+5
source share
1 answer

I found a problem and a solution in part. As pointed out in my question, the device was Lollipop(5.1.1) . When I use the emulator to test Marshmallow(6.0) , it worked like a charm, without any changes. An emulator error also fails for Lollipop .

So for future reference:

Android did not support socks (using HURL) on devices prior to Marshmellow.

+1
source

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


All Articles