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

相关推荐
小贾要学习5 小时前
【Linux】TCP网络通信编程
linux·服务器·网络·c++·网络协议·tcp/ip
Hello_Embed5 小时前
嵌入式上位机开发入门(十九):Socket 状态检测与断线重连
网络·单片机·网络协议·tcp/ip·嵌入式
cheems95276 小时前
[SpringMVC]Cookie 和Session 从无状态协议到状态保存实务
网络·http
添砖java‘’6 小时前
网络层IP
网络·网络协议·tcp/ip·ip
灰子学技术8 小时前
Envoy 底层 TCP 交互、UDS 和事件驱动技术文档
网络·网络协议·tcp/ip
试试勇气9 小时前
C++实现json-rpc框架
网络协议·rpc·json
CDN36010 小时前
高防服务器磁盘 / CPU 爆满?攻击引流与资源扩容实战
运维·服务器·网络协议
听到微笑10 小时前
MCP传输协议演进:从SSE到Streamable HTTP
网络·网络协议·http
哇蛙蛙10 小时前
H3CNE--23.ACL
服务器·网络·经验分享·网络协议·tcp/ip·h3cne
IT枫斗者11 小时前
MSE Nacos Prompt 管理:AI Agent 配置的工程化治理实践
网络·人工智能·websocket·网络协议·prompt·jar