0

I'm getting javax.net.ssl.SSLHandshakeException: Connection closed by peer when i try to make https request from android app using HttpsURLConnection Above the code for to make request and return a Bitmap.

protected Bitmap doInBackground(String... params) {
    try {

        // Create a trust manager that does not validate certificate chains
        TrustManager[] trustAllCerts = new TrustManager[] {
                new X509TrustManager() {
                    public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                        return new X509Certificate[0];
                    }
                    public void checkClientTrusted(
                            java.security.cert.X509Certificate[] certs, String authType) {
                    }
                    public void checkServerTrusted(
                            java.security.cert.X509Certificate[] certs, String authType) {
                    }
                }
        };

        // Install the all-trusting trust manager
        try {
            SSLContext sc = SSLContext.getInstance("SSL");
            sc.init(null, trustAllCerts, new java.security.SecureRandom());
            HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
        } catch (GeneralSecurityException e) {
            e.printStackTrace();
        }

        HttpsURLConnection connection = (HttpsURLConnection)
                new URL("www.example.com.br").openConnection();
        connection.setHostnameVerifier(getHostnameVerifier("www.example.com.br");
        connection.setRequestMethod("GET");
        connection.setConnectTimeout(3000);
        connection.setUseCaches(false);
        connection.setAllowUserInteraction(false);
        connection.connect();
        setCookieActivity(connection.getHeaderField("Set-Cookie"));
        return BitmapFactory.decodeStream(connection.getInputStream());
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}



private HostnameVerifier getHostnameVerifier(final String url) {
    HostnameVerifier hostnameVerifier = new HostnameVerifier() {
        @Override
        public boolean verify(String hostname, SSLSession session) {
            HostnameVerifier hv =
                    HttpsURLConnection.getDefaultHostnameVerifier();
            return hv.verify(url, session);
        }
    };
    return hostnameVerifier;
}

I already tried many thing, but i get the same exception

This link works for many, but not for me.

telling java to accept self-signed ssl certificate

Community
  • 1
  • 1
Igor Ronner
  • 1,565
  • 2
  • 14
  • 31
  • I would try a HEAD request or try to read a small resource first to eliminate simple time out errors. Or does the error occur immediately? – mjn Jun 14 '16 at 17:48
  • the error occur immediately. – Igor Ronner Jun 14 '16 at 18:03
  • I would try Volley which is included in newer Android versions - see https://developer.android.com/training/volley/index.html - maybe with a separate project to check if it has the same problem – mjn Jun 14 '16 at 18:10

0 Answers0