3

Ich versuche, eine Videodatei mit einer signierten URL in den Cloud-Speicher hochzuladen. Die HTTP-Put-Methode wird zum Hochladen verwendet. Wenn ich versuche, eine Verbindung mit "HttpsUrl" Verbindung herzustellen, gibt es einen Fehler wie zurück javax.net.ssl.SSLHandshakeException: Handshake fehlgeschlagen. Wie kann ich dieses Problem lösen? Hier ist mein Code:Medien Upload in die Cloud mit signierten URL Interup aufgrund einiger SSL-Zertifikat Probleme?

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 ist wie dieser

javax.net.ssl.SSLHandshakeException: Handshake failed  
com.android.org.conscrypt.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:390c) 
com.android.okhttp.Connection.upgradeToTls(Connection.java:201) 

Antwort

6

> Schreiben Sie den Code für die Überprüfung von SSL vermeiden

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); 

} 


} 

Ergänzen Sie die unter dem Code vor dem Öffnen der Verbindung

URL url = new URL(urlString); 
DisableSSL disable = new DisableSSL(); 
disable.disableSSLVerification(); 
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection(); 
connection.connect();