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

标签 android https sslhandshakeexception

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

这是我尝试发送发布请求的代码 fragment :

   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 - SSLHandshakeException - Chain链验证失败,如何解决?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53265234/

相关文章:

android - 我应该在android中使用什么推送通知

android - 在 framelayout 中捏合缩放谷歌地图而不移动标记

ssl - 如何忽略服务器上的 TLS 证书

java - 调用安全 REST 服务时出现 SSLHandshakeException

eclipse - Gradle 导入 Sun 安全异常

android - 在主屏幕上创建 Android 应用程序的快捷方式

android - 生成具有所有动态功能的 APK

python - 是否可以检查出站 https 流量?

java - 间接 Apache HttpClient 和 javax.net.ssl.SSLException : hostname in certificate didn't match

启用 TLSv1.3 的 Java 11 和 12 SSL 套接字因 handshake_failure 错误而失败