How to use HttpURLConnection over SOCKS proxy on Android
<p>Well, I have been trying to use SOCKS proxy on android. Too much I am struggled and I failed to use it correctly.</p>

<p>Basically I do want to get a content of a page over a SOCKS proxy using <code>HttpURLConnection</code> (is a must).</p>

<p>The code I am using is this:</p>

<pre><code>    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 &gt;= 0; x = is.read()) {
        System.out.print((char) x);
    }
    System.out.println();
</code></pre>

<p>I do get <code>SocketTimeOutException</code>, here is the stack trace:</p>

<pre><code>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)
</code></pre>

<p>Some curious things:</p>

<ul>
<li>The proxy points to my SSH tunnel (apparently tested on other devices using firefox socks proxy feature).</li>
<li>When I change <code>Proxy.Type</code> to <code>HTTP</code> and choose an appropriate HttpProxy from internet it works, but I need socks.</li>
<li>Don't mind <code>System.out.print</code> and the loop, it is there for test purpose.</li>
<li>Test device is a C6603 model and Android version 5.1.1</li>
</ul>

<hr>

<p>Edit:
It seems that SOCKS proxy does not work with <code>HttpURLConnection</code> on android. Some references:
This <a href="https://www.mail-archive.com/guardian-dev%40lists.mayfirst.org/msg01006.html" rel="nofollow">thread</a> and this <a href="http://guardian-dev.mayfirst.narkive.com/aBGKAwSG/netcipher-and-httpurlconnection" rel="nofollow">thread</a> claims so. I want to be sure if there is a way without using 3rd party libraries.</p>