Android Studio - 服务器证书过期导致抛出“错误:原因:无法找到请求目标的有效证书路径”。
创始人
2024-08-15 22:30:15
0

当使用Android Studio进行网络请求时,有时会遇到服务器证书过期的问题,导致抛出“错误:原因:无法找到请求目标的有效证书路径”的异常。以下是一种解决方法,其中包含了代码示例。

解决方法:

  1. 创建一个自定义的SSLSocketFactory类,用于处理证书验证的问题。在该类中,我们可以重新实现验证服务器证书的逻辑。
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

public class CustomSSLSocketFactory {
    public static void disableCertificateValidation() {
        try {
            TrustManager[] trustAllCerts = new TrustManager[]{
                    new X509TrustManager() {
                        public X509Certificate[] getAcceptedIssuers() {
                            return new X509Certificate[0];
                        }
                        public void checkClientTrusted(X509Certificate[] certs, String authType) throws CertificateException {}
                        public void checkServerTrusted(X509Certificate[] certs, String authType) throws CertificateException {}
                    }
            };

            SSLContext sc = SSLContext.getInstance("TLS");
            sc.init(null, trustAllCerts, new SecureRandom());
            HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());

            HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
                public boolean verify(String hostname, SSLSession session) {
                    return true;
                }
            });
        } catch (NoSuchAlgorithmException | KeyManagementException e) {
            e.printStackTrace();
        }
    }
}
  1. 在进行网络请求之前,调用CustomSSLSocketFactory类的disableCertificateValidation()方法,禁用证书验证。
CustomSSLSocketFactory.disableCertificateValidation();

// 进行网络请求的代码示例
URL url = new URL("https://example.com");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
...

通过以上方法,我们可以禁用Android Studio对服务器证书的验证,从而解决"错误:原因:无法找到请求目标的有效证书路径"异常。请注意,禁用证书验证存在安全风险,请在安全可靠的环境中使用。

相关内容

热门资讯

分享透视!aapoker俱乐部... 分享透视!aapoker俱乐部靠谱吗,微乐麻将自建房app,秘籍教程(真的有挂)-哔哩哔哩aapok...
教你透视!aapoker辅助怎... 教你透视!aapoker辅助怎么用,微信小程序游戏充值破解,指引教程(有挂详情)-哔哩哔哩1)aap...
分享透视!hhpoker免费透... 分享透视!hhpoker免费透视脚本,小程序微乐贵阳捉鸡麻将挂,办法教程(有挂详情)-哔哩哔哩1、在...
详情透视!wepoker到底有... 详情透视!wepoker到底有没有透视,微乐小程序自建房辅助,指南书教程(有挂教学)-哔哩哔哩小薇(...
详情透视!wepoker科技辅... 详情透视!wepoker科技辅助器,微乐智能辅助软件是真的吗,模板教程(有挂方法)-哔哩哔哩进入游戏...
曝光透视!wepokerplu... 曝光透视!wepokerplus透视脚本免费,微乐小程序游戏破解器,模板教程(有挂攻略)-哔哩哔哩1...
必备透视!wepoker私人局... 必备透视!wepoker私人局怎么玩,微乐麻将脚本掌上程序,模块教程(竟然有挂)-哔哩哔哩一、wep...
总结透视!德扑圈透视挂,微乐自... 总结透视!德扑圈透视挂,微乐自建房脚本使用安全吗,窍要教程(有挂实锤)-哔哩哔哩1、游戏颠覆性的策略...
辅助透视!We poker辅助... 辅助透视!We poker辅助器下载,微乐钻石兑换码,策略教程(发现有挂)-哔哩哔哩1、进入到We ...
解谜透视!sohoo开挂辅助,... 解谜透视!sohoo开挂辅助,万能透视辅助器免费版,要领教程(有挂神器)-哔哩哔哩1)sohoo开挂...