SSLHandshakeException-链链验证失败,如何解决?

时间:2018-11-12 15:25:02

标签: android https sslhandshakeexception

在我的应用程序中,我试图向服务器发出HTTPS POST请求。 但是,我一直在获取SSLHandshakeException-链链验证始终失败。我试图使用POSTMAN发送请求,但服务器收到了响应。当我尝试从应用程序发送请求时,可能导致此错误的原因是什么?

下面是我尝试发送帖子请求的代码段:

   public static JSONObject getDataLibConfiguration(Context context) throws HttpRequestException {

    int statusCode = 0;

    JSONObject commonInformation;
    HttpsURLConnection connection = null;

    try {

        commonInformation = ConfigurationProcessor.getCommonInformation(context);
        if (commonInformation == null) {
            return null;
        }

        URL url = new URL(BuildConfig.SERVER_CONFIG_URL);
        if (BuildConfig.DEBUG) {
            LogUtils.d(TAG, "url = " + url.getPath());
        }

        connection = getHttpsConnection(url);
        connection.setDoOutput(true);
        connection.setDoInput(true);
        connection.setRequestMethod("POST");
        connection.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
        connection.setRequestProperty("Content-Encoding", "gzip");

        byte[] gzipped = HttpUtils.gzip(commonInformation.toString());
        cos = new CountingOutputStream(connection.getOutputStream()); //<-- This is where I get the exception
        cos.write(gzipped);
        cos.flush();

        statusCode = connection.getResponseCode();
        // More code her
 }

private static HttpsURLConnection getHttpsConnection(URL url) throws IOException, GeneralSecurityException {

        HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();

        try {
            SSLContext sslContext = SSLContext.getInstance("TLS");
            MatchDomainTrustManager myTrustManager = new MatchDomainTrustManager(url.getHost());
            TrustManager[] tms = new TrustManager[]{myTrustManager};
            sslContext.init(null, tms, null);
            SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
            connection.setSSLSocketFactory(sslSocketFactory);
        } catch (AssertionError ex) {
            if (BuildConfig.DEBUG) {
                LogFileUtils.e(TAG, "Exception in getHttpsConnection: " + ex.getMessage());
            }
            LogUtils.e(TAG, "Exception: " + ex.toString());
        }
        return connection;
    }

7 个答案:

答案 0 :(得分:3)

我的日期和时间是正确的,但我的系统设置中没有选中“使用网络提供的时间”。

我通过转到设置 > 日期和时间 > 选中“使用网络提供的时间”并选中“使用网络提供的时区”来解决此问题。

然后这个错误就消失了。

答案 1 :(得分:2)

如果有人遇到与特定设备有关的问题,则原因应该是由于设备中设置的日期不正确。

答案 2 :(得分:1)

就我而言,这是错误的电话约会。

修复日期解决了一个问题

答案 3 :(得分:0)

问题是证书已过期。

答案 4 :(得分:0)

public static void trustEveryone() {
        try {
            HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier(){
                public boolean verify(String hostname, SSLSession session) {
                    return true;
                }});
            SSLContext context = SSLContext.getInstance("TLS");
            context.init(null, new X509TrustManager[]{new X509TrustManager(){
                public void checkClientTrusted(X509Certificate[] chain,
                                               String authType) throws CertificateException {}
                public void checkServerTrusted(X509Certificate[] chain,
                                               String authType) throws CertificateException {}
                public X509Certificate[] getAcceptedIssuers() {
                    return new X509Certificate[0];
                }}}, new SecureRandom());
            HttpsURLConnection.setDefaultSSLSocketFactory(
                    context.getSocketFactory());
        } catch (Exception e) { // should never happen
            e.printStackTrace();
        }
    }

或检查设备的系统日期-当我尝试用错误的日期连接时出现此异常!..

答案 5 :(得分:0)

如果您使用的是仿真设备,只需“冷启动”它就可以解决问题。

如果您让它们运行一段时间,有时这些日期可能会卡住,这将导致此证书过期问题。

答案 6 :(得分:-3)

我通过将api的基本URL从“ https”更改为“ http”来解决了该问题

相关问题