3

署名付き URL を使用してビデオ ファイルをクラウド ストレージにアップロードしようとしています。アップロードには HTTP put メソッドを使用します。「HttpsUrl`接続」を使用して接続しようとすると、javax.net.ssl.SSLHandshakeException: Handshake failedのようなエラーが返されます。この問題を解決するにはどうすればよいですか? これが私のコードです:

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

スタックトレースはこんな感じ

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

1 に答える 1

6

> SSL検証を回避するコードを書く

パブリック クラス 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);

}


}

接続を開く前に以下のコードを追加します

URL url = new URL(urlString);
DisableSSL disable = new DisableSSL();
disable.disableSSLVerification();
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
connection.connect();
于 2016-04-13T05:36:29.240 に答える