vallllll
vallllll

Reputation: 2761

Android HttpsURLConnection throws IllegalStateException only on Android 4.4.2 nexus devices

I have th following code which works on many Android device (Motorola Xoom (4.0.4, Samsung Galaxy S3 (4.2.2), Samsung galaxy tab 3 (4.3)) but it does no work on nexus 7, nor nexus 4 both (4.4.2) so either nexus devices problem or the 4.4.2 android version.

httpsURLConnection = (HttpsURLConnection)url.openConnection();

if(socketFactory!=null){
    httpsURLConnection.setSSLSocketFactory(socketFactory);
} else {
   log.w("tryConnecting : socket factory is null");
}
httpsURLConnection.setHostnameVerifier(new MzHostNameVerifier());

httpsURLConnection.connect();
int responseCode = httpsURLConnection.getResponseCode();
if(responseCode == HttpURLConnection.HTTP_OK){
Certificate[] certificate = httpsURLConnection.getServerCertificates();
httpsURLConnection.disconnect();
return certificate;
} else {
log.e("Connection error, code : "+responseCode);
return null;
}

I get exception java.lang.IllegalStateException: Connection has not yet been established on line :

   **Certificate[] certificate = httpsURLConnection.getServerCertificates();**

I don't uderstand why and also why it works on other devices and not nexus 7/nexus 4. I need the certificate to make some checks with it, what could I do? Is there something missing?

Thanks a lot

Upvotes: 2

Views: 1404

Answers (1)

Ocean Airdrop
Ocean Airdrop

Reputation: 2921

I also had this exact same issue.

My code worked on a Samsung S3 but failed with the exception "java.lang.IllegalStateException: Connection has not yet been established" on a Samsung S4.

To fix the issue call getServerCertificates() after reading some data from the connection.

Before making your call to getServerCertificates() make a call to getInputStream() before to perform some i/o over the connection.

Please see example below:

enter image description here

private boolean ValidateHTTPSCertificate()
    {
        boolean result = false;
        String serverCertPublicSerial = "abcdefghijklmnopqrstuvwxyz";

        try
        {
            URL url = new URL( "https://your-url-goes-here" );
            HttpsURLConnection con = (HttpsURLConnection) url.openConnection();

            if(con!=null){

                try {
                    con.connect();

                    int respCode =  con.getResponseCode();
                    String pageResult = convertInputStreamToString(con.getInputStream());

                    Certificate[] certs = con.getServerCertificates();
                    for(Certificate cert : certs){

                        X509Certificate x509cert = (X509Certificate)cert;

                        BigInteger serialNum = x509cert.getSerialNumber();

                        String name = x509cert.getIssuerDN().getName();
                        String publicKeySerial = serialNum.toString(16);

                        if (publicKeySerial.toLowerCase().equals(serverCertPublicSerial.toLowerCase()) == true )
                        {
                            result = true;
                            break;
                        }
                    }
                    //con.disconnect();
                } catch (SSLPeerUnverifiedException e) {
                    e.printStackTrace();
                } catch (IOException e){
                    e.printStackTrace();
                }
            }
        }
        catch (Exception ex)
        {
            String  msg = ex.getMessage();
        }

        return result;
    }

Upvotes: 2

Related Questions