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

问题描述 投票:0回答:13

在我的应用程序中,我尝试向我的服务器发出 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;
    }
android https sslhandshakeexception
13个回答
224
投票

就我而言,手机上的日期是错误的。

修复解决问题的日期


48
投票

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

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


34
投票

问题是证书已过期。


33
投票

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


15
投票

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


14
投票

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

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

然后这个错误就消失了。


10
投票
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();
        }
    }

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


7
投票

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


6
投票

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


2
投票

如果你使用android模拟器,可以擦除数据并再次运行,就可以了


1
投票

@Yash Bhardwaj 在@Vadim 回答的评论中说问题出在 Glide 框架中。我遇到了同样的问题:使用 Ktor 框架对服务器的 https 请求全部成功,但是当 Glide 尝试从同一服务器加载图像时,它面临

SSLHandshakeException
。 要解决此问题,您应该查看此处:Solve Glide SSLHandshakeException

要处理

@GlideModule
注释,您应该导入
kapt
插件并将这些依赖项添加到您的应用程序中
build.gradle
:

implementation 'com.github.bumptech.glide:okhttp3-integration:4.11.0'
kapt 'com.github.bumptech.glide:compiler:4.12.0'

0
投票

就我而言,日期/时间没问题,但我仍然遇到异常。我重新启动了设备,问题就消失了。如果以上方法均失败,希望对某人有所帮助。


0
投票

在我的例子中,SSL 未实现,并且

s
被错误添加到 URL 中的
http
之后。

从 URL 中删除

s
,它就成功了。

https://example.com 更新为 http://example.com

© www.soinside.com 2019 - 2024. All rights reserved.