Android Java: HttpURLConnection with Proxy (unexpected end of stream on null)

Viewed 29

I am trying to fetch data from a remote server via a service of an Android app. I need to use a https-proxy-server with basic auth for this.

On a windows machine, the following code works perfectly fine:

                Authenticator authenticator = new Authenticator() {
                    @Override
                    public PasswordAuthentication getPasswordAuthentication() {
                        return (new PasswordAuthentication("USER", "PASS".toCharArray()));
                    }
                };

                Authenticator.setDefault(authenticator);

                Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("my.proxy.url", 80));

                HttpURLConnection connection = (HttpURLConnection) new URL("www.url.com").openConnection(proxy);

                connection.setInstanceFollowRedirects(false);

                connection.connect();
                int code = connection.getResponseCode();
                String RedirectHeader = connection.getHeaderField("Location");

I just had to call this in main:

System.setProperty("jdk.http.auth.tunneling.disabledSchemes", "");

Now when I try to do the same thing on an Android-Device (it doesn't matter if it is an emulator or a real device) I recieve the following error message at the connect() method:

W/System.err: java.io.IOException: unexpected end of stream on null
    at com.android.okhttp.internal.http.Http1xStream.readResponse(Http1xStream.java:203)
    at com.android.okhttp.internal.io.RealConnection.createTunnel(RealConnection.java:274)
    at com.android.okhttp.internal.io.RealConnection.connectTls(RealConnection.java:176)
W/System.err:     at     com.android.okhttp.internal.io.RealConnection.connectSocket(RealConnection.java:153)
    at com.android.okhttp.internal.io.RealConnection.connect(RealConnection.java:116)
    at     com.android.okhttp.internal.http.StreamAllocation.findConnection(StreamAllocation.java:186)
    at     com.android.okhttp.internal.http.StreamAllocation.findHealthyConnection(StreamAllocation.java:128)
    at com.android.okhttp.internal.http.StreamAllocation.newStream(StreamAllocation.java:97)
    at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:289)
W/System.err:     at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:232)
    at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:465)
    at com.android.okhttp.internal.huc.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:131)
    at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.connect(DelegatingHttpsURLConnection.java:90)
    at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.connect(HttpsURLConnectionImpl.java:30)

When I remove the "proxy"-variable in the "openConnection()"-function, everything works fine - it seems to be a problem with the proxy itself.

PS: I tried with JSOUP but cannot get a proxy to work since I need to set the proxy for SOME SPECIFIC requests only and not for the entire application. Also I cannot use OKHTTP because it seems to append some sort of headers that get blocked by the remote server because it thinks my application is a crawler.

0 Answers
Related