io 在服务器上,客户端在 android 上。
只要我启用 HTTP(S) SSL,它就会导致 android 上的连接错误(如果禁用它则工作正常)
我尝试在Android上实现HTTPS连接,引用了Github上的示例如下:
opts = new IO.Options();
opts.sslContext = mySSLContext;
opts.hostnameVerifier = myHostnameVerifier;
socket = IO.socket("https://mychat.url", opts);
还有这个
SSLContext mySSLContext = SSLContext.getInstance("TLS");
mySSLContext.init(null, null, null);
还有这个
myHostnameVerifier = new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
return true;
}
});
在套接字传输期间仍然收到错误消息
io.socket.engineio.client.EngineIOException: xhr poll error
javax.net.ssl.SSLHandshakeException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.
用HTTPS协议(protocol)实现socket io连接需要什么?
最佳答案
感谢@Apurva 我自己解决了它。
似乎 TrustManager 是初始化 sslContext 所必需的
所以我加了
mySSLContext.init(null, trustAllCerts, null);
用参数trustAllCerts引用
private final TrustManager[] trustAllCerts= new TrustManager[] { new X509TrustManager() {
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return new java.security.cert.X509Certificate[] {};
}
public void checkClientTrusted(X509Certificate[] chain,
String authType) throws CertificateException {
}
public void checkServerTrusted(X509Certificate[] chain,
String authType) throws CertificateException {
}
} };
终于成功连接到聊天服务器。
关于android - 使用 HTTPS 协议(protocol)的 android 连接中的 Socket.io 客户端失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37826728/