android - 使用 HTTPS 协议(protocol)的 android 连接中的 Socket.io 客户端失败?

标签 android ssl https socket.io tls1.2

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/

相关文章:

android - Android应用程序可以向其他手机发送数据包吗?

security - 如何保持公钥静态,但轮换证书

wordpress - 我安装了新的 SSL,现在只有一半的站点是安全的?

ssl - Cname 重定向到 heroku?

android - 点击当前选项卡刷新页面时的 Xamarin.Forms TabbedPage 事件

Android: Maven 最终存档名称

apache - 将 HTTPS 443 端口重定向到其他服务器上的多个站点

amazon-web-services - Amazon CloudFront 和 HTTPsL 我该怎么办?

java - Appengine 上的 Twilio 客户端库

android - Gradle汇编任务后,AndroidManifest.xml编码不可读