使用HTTPS协议的android连接中的Socket.io客户端失败? [英] Socket.io client in android connection with HTTPS protocol failed?

查看:496
本文介绍了使用HTTPS协议的android连接中的Socket.io客户端失败?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在服务器上的io在android上.

io on server and client on android.

只要我启用HTTP(S)SSL,它就会在android上导致连接错误(但是,如果禁用它,则可以正常工作)

It results in connection error on android as long as I enable HTTP(S) SSL(works fine if disable it however)

我试图在Android上实现HTTPS连接,并从Github上的示例中引用了以下内容:

I've tried to implement HTTPS connection on Android, took reference from sample on Github as following:

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;
    }
});

在套接字传输过程中仍然收到错误消息

Still got error message during socket transport

io.socket.engineio.client.EngineIOException: xhr poll error

javax.net.ssl.SSLHandshakeException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.

使用HTTPS协议实现套接字io连接需要做什么?

What does it take to fulfill socket io connection with HTTPS protocol?

推荐答案

感谢@Apurva,我自己解决了此问题.

Thanks to @Apurva I resolve it on my own.

似乎必须使用TrustManager来初始化sslContext

Seems that TrustManager is necessary to initialize sslContext

所以我添加了

mySSLContext.init(null, trustAllCerts, null);

带有参数trustAllCerts的参考

with parameter trustAllCerts refer to

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 {
    }
} };

最后成功连接到聊天服务器.

And finally connected to chat server successfully.

这篇关于使用HTTPS协议的android连接中的Socket.io客户端失败?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆