HttpsUrl连接:SSL Handshke异常

时间:2016-10-27 11:28:31

标签: android api ssl ssl-certificate httpsurlconnection

javax.net.ssl.SSLHandshakeException: javax.net.ssl.SSLProtocolException: SSL handshake aborted: ssl=0xb7e0b598: Failure in SSL library, usually a protocol error
error:14077410:SSL routines:SSL23_GET_SERVER_HELLO:sslv3 alert handshake failure (external/openssl/ssl/s23_clnt.c:741 0x9dcb9d4d:0x00000000)

仅在三星设备中HTTPSUrlconnection发生上述异常。我已尝试使用sele签名证书(.crt.bks),但api无法在Samsung设备上运行。

你能建议一些出路吗?

源代码:
----------------- HTTPS URL连接----------------- public static String [] get_httpurlconnection(Context context,String Url)             抛出ConnectTimeoutException {

    String[] result = { "", "" };
    try {

        HttpsURLConnection urlConnection = getConnection1(Url, context);
        urlConnection.setRequestMethod("GET");
        urlConnection.setConnectTimeout(connection_timeout);
        urlConnection.setRequestProperty("Content-Type",
                "application/x-www-form-urlencoded");
        if (Build.VERSION.SDK_INT > 13) {
            urlConnection.setRequestProperty("Connection", "close");
        }

        // urlConnection.setUseCaches(false);
        // urlConnection.setDoInput(true);
        // urlConnection.setDoOutput(true);

        urlConnection.connect();
        // always check HTTP response code first
        int responseCode = urlConnection.getResponseCode();
        result[0] = responseCode + "";

        if (responseCode == HttpURLConnection.HTTP_OK) {
            // Get Response
            InputStream is = urlConnection.getInputStream();
            BufferedReader rd = new BufferedReader(
                    new InputStreamReader(is));
            String line;
            StringBuffer response = new StringBuffer();
            while ((line = rd.readLine()) != null) {
                response.append(line);
                response.append('\r');
            }
            rd.close();

            if (!TextUtils.isEmpty(response)) {
                result[0] = HttpConnectionUrl.RESPONSECODE_REQUESTSUCCESS;
                result[1] = response.toString();
                Log.i("TAG_RESPONSE : ", result[1]);
            }

        }

    } catch (UnsupportedEncodingException e) {
        result[0] = HttpConnectionUrl.RESPONSECODE_CONNECTIONTIMEOUT;
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        result[0] = HttpConnectionUrl.RESPONSECODE_CONNECTIONTIMEOUT;
        e.printStackTrace();
    } catch (ConnectTimeoutException e) {
        result[0] = HttpConnectionUrl.RESPONSECODE_CONNECTIONTIMEOUT;
        e.printStackTrace();
    } catch (IOException e) {
        result[0] = HttpConnectionUrl.RESPONSECODE_CONNECTIONTIMEOUT;
        e.printStackTrace();
    } catch (Exception e) {
        result[0] = HttpConnectionUrl.RESPONSECODE_CONNECTIONTIMEOUT;
        e.printStackTrace();
    }
    return result;
}

public static HttpsURLConnection getConnection1(String apivalue,
        Context mContext) throws IOException {
    try {
        URL url = new URL(apivalue);
        HttpsURLConnection urlConnection = (HttpsURLConnection) url
                .openConnection();
        KeyStore trustStore = KeyStore.getInstance("BKS");
        InputStream trustStoreStream = mContext.getResources()
                .openRawResource(R.raw.qamcorp);
        trustStore.load(trustStoreStream, "123456".toCharArray());

        TrustManagerFactory trustManagerFactory = TrustManagerFactory
                .getInstance(KeyManagerFactory.getDefaultAlgorithm());
        trustManagerFactory.init(trustStore);

        SSLContext sslContext = SSLContext.getInstance("TLS");
        sslContext.init(null, trustManagerFactory.getTrustManagers(), null);
        SSLSocketFactory factory = sslContext.getSocketFactory();
        SSLSocket socket = (SSLSocket) factory.createSocket();
        socket.setEnabledCipherSuites(SSLUtils
                .getCipherSuitesWhiteList(socket.getEnabledCipherSuites()));
        urlConnection.setSSLSocketFactory(factory);

        return urlConnection;
    } catch (GeneralSecurityException e) {

        throw new IOException("Could not connect to SSL Server", e);
    }
}

0 个答案:

没有答案