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

Kes*_*lme 34 https android 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;
    }
Run Code Online (Sandbox Code Playgroud)

Vad*_*dim 81

就我而言,电话日期错误。

修复日期解决了一个问题

  • Android 12 模拟器上出现问题的原因是相同的。模拟器的冷启动对我有用。 (3认同)
  • 我在使用 Android 电视模拟器时遇到了同样的问题。更新日期并设置正确的时区解决了问题。 (2认同)

小智 49

如果您使用的是模拟设备,只需“冷启动”即可解决问题。

有时,如果您让这些东西运行一段时间,它们的日期可能会被卡住,从而导致证书过期问题。


Md *_*ury 33

就我而言,我在 Android 模拟器上获取此问题。当我清除模拟器缓存后,问题就解决了。

在此输入图像描述


Kes*_*lme 18

问题是证书已过期。

  • 就我而言,我的模拟器日期未更新。晚了几个星期 (4认同)

Hay*_*yan 15

就我而言,问题出在电话日期上。所以请检查一下,设置为自动。


har*_*ren 7

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


小智 6

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();
        }
    }
Run Code Online (Sandbox Code Playgroud)

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


小智 6

我通过重置模拟器日期和时间修复了此错误。我的服务器工作正常,只是我将模拟器的日期和时间更改为当前服务器时区。


zet*_*las 5

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

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

然后这个错误就消失了。