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 回答