android https 证书过期

有的时候 我们android https 证书过期 ,或者使用明文等方式去访问服务器 可能会碰到类似的 问题 :

javax.net.ssl.SSLHandshakeException: Chain validation failed

java.security.cert.CertPathValidatorException: Response is unreliable: its validity interval is out-of-date

java.security.cert.CertPathValidatorException: Could not determine revocation status

所以我们 尝试使用一下方式解决 :

1) 忽略证书验证

TrustManager[] trustAllCerts = new TrustManager[] {

new X509TrustManager() {

public void checkClientTrusted(X509Certificate[] chain, String authType) {}

public void checkServerTrusted(X509Certificate[] chain, String authType) {}

public X509Certificate[] getAcceptedIssuers() {

return new X509Certificate[0];

}

}

};

try {

SSLContext sslContext = SSLContext.getInstance("TLS");

sslContext.init(null, trustAllCerts, new java.security.SecureRandom());

HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());

// 进行网络请求...

} catch (Exception e) {

e.printStackTrace();

}

如果您希望允许整个应用程序使用不安全的 HTTP 连接,可以在应用程序的清单文件(AndroidManifest.xml)中添加以下标记:

复制代码

<application

android:usesCleartextTraffic="true" ...>

...

</application>

更详细的可以去问 gpt

相关推荐
韭菜炒大葱1 天前
前端经典面试题:从 URL 输入到页面展示,中间经历了什么?
前端·http·面试
Sheffield2 天前
Docker的跨主机服务与其对应的优缺点
linux·网络协议·docker
小时前端7 天前
HTTPS 页面加载 HTTP 脚本被拦?同源代理来救场
前端·https
YuMiao7 天前
gstatic连接问题导致Google Gemini / Studio页面乱码或图标缺失问题
服务器·网络协议
不可能的是8 天前
前端 SSE 流式请求三种实现方案全解析
前端·http
Jony_9 天前
高可用移动网络连接
网络协议
chilix10 天前
Linux 跨网段路由转发配置
网络协议
gihigo199811 天前
基于TCP协议实现视频采集与通信
网络协议·tcp/ip·音视频
古译汉书12 天前
【IoT死磕系列】Day 7:只传8字节怎么控机械臂?学习工业控制 CANopen 的“对象字典”(附企业级源码)
数据结构·stm32·物联网·http
白太岁12 天前
通信:(5) 电路交换、报文交换与分组交换
运维·服务器·网络·网络协议