3

I am trying to upload a video file to cloud storage using a signed URL. HTTP put method is used for uploading. When I am try to connect using "HttpsUrl` connection", it returns some error like javax.net.ssl.SSLHandshakeException: Handshake failed. How can I resolve this problem? Here is my code:

URL url = new URL(url_string);
httpsUrlConnection = (HttpsURLConnection) url.openConnection();
httpsUrlConnection.setDoOutput(true);
httpsUrlConnection.setDoInput(true);
httpsUrlConnection.setRequestMethod(requestMethod);
httpsUrlConnection.setRequestProperty("Content-Type", "application/json");
httpsUrlConnection.setRequestProperty("Accept", "application/json");            
httpsUrlConnection.connect();

stacktrace is like this

javax.net.ssl.SSLHandshakeException: Handshake failed     
com.android.org.conscrypt.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:390c)
com.android.okhttp.Connection.upgradeToTls(Connection.java:201)
anas p a
  • 383
  • 5
  • 20

1 Answers1

6

> Write the code for avoid SSL verification

public class DisableSSL {

public void disableSSLVerification() {

    TrustManager[] trustAllCerts = new TrustManager[]{new X509TrustManager() {
        public java.security.cert.X509Certificate[] getAcceptedIssuers() {
            return null;
        }

        public void checkClientTrusted(X509Certificate[] certs, String authType) {
        }

        public void checkServerTrusted(X509Certificate[] certs, String authType) {
        }

    }};

    SSLContext sc = null;
    try {
        sc = SSLContext.getInstance("SSL");
        sc.init(null, trustAllCerts, new java.security.SecureRandom());
    } catch (KeyManagementException e) {
        e.printStackTrace();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());

    HostnameVerifier allHostsValid = new HostnameVerifier() {
        public boolean verify(String hostname, SSLSession session) {
            return true;
        }
    };
    HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);

}


}

add the below code before open connection

URL url = new URL(urlString);
DisableSSL disable = new DisableSSL();
disable.disableSSLVerification();
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
connection.connect();
anas p a
  • 383
  • 5
  • 20