Search code examples
androidhttpsproxyhttpurlconnectionfiddler

Android HttpURLConnection with fiddler: IOException: socket is closed


I set up Fiddler2 for Android emulator, it works, I can see the HTTPS traffic through Android browser, but I cannot connect to HTTPS server with my code using HttpsURLConnection:

    // use Fiddler's proxy:
    Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("172.16.4.20", 8888));
    HttpsURLConnection connection = (HttpsURLConnection) new URL(url)
            .openConnection(proxy);

    connection.setRequestMethod(method);
    connection.setRequestProperty("Content-Type", contentType);
    connection.setDoInput(true);
    connection.setDoOutput(true);
    OutputStream out = connection.getOutputStream();

I got:

java.net.SocketException: Socket is closed

In Fiddler I can see the first request (I removed the host address)

CONNECT [address]:443 HTTP/1.1
Host: [address] 
User-Agent: Dalvik/1.6.0 (Linux; U; Android 4.0.4; Android SDK built for x86 Build/IMM76D)
Connection: Keep-Alive

After the client received notice of the established CONNECT, it failed to send any data.

Response:

HTTP/1.1 200 Connection Established
FiddlerGateway: Direct
StartTime: 12:32:18.567
Connection: close

The above request response pair is like any other Android browser HTTPS connection establishment.

After that I cannot send my POST request (because I got the SocketException). Without proxy it works.


Solution

  • The most likely explanation is that the client is closing the connection because it's not satisfied with Fiddler's HTTPS decryption certificate. Have you configured the device to trust the FiddlerRoot.cer certificate file? Have you attached an event handler to your HttpsURLConnection to accept Fiddler's self-signed certificate?