Socket.io android 中的客户端与 HTTPS 协议的连接失败?

Socket.io client in android connection with HTTPS protocol failed?

服务器上的 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协议实现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 {
    }
} };

终于成功连接到聊天服务器了。

感谢@Dayo Choul,我通过 https 建立了 socket.io 连接。我正在阅读 https://github.com/socketio/socket.io-client-java,但它并没有告诉您所需的一切。 post 仍然缺少这些位,所以我决定 post 我的代码供任何可能觉得它有用的人使用。更改地址和端口以满足您的需要。

HostnameVerifier myHostnameVerifier = new HostnameVerifier() {
    @Override
    public boolean verify(String hostname, SSLSession session) {
        return true;
    }
};
TrustManager[] trustAllCerts= new TrustManager[] { new X509TrustManager() {
    public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
    }

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

    public X509Certificate[] getAcceptedIssuers() {
        return new X509Certificate[0];
    }
}};

SSLContext mySSLContext = null;
try {
    mySSLContext = SSLContext.getInstance("TLS");
    try {
        mySSLContext.init(null, trustAllCerts, null);
    } catch (KeyManagementException e) {
        e.printStackTrace();
    }
} catch (NoSuchAlgorithmException e) {
    e.printStackTrace();
}
OkHttpClient okHttpClient = new OkHttpClient.Builder().hostnameVerifier(myHostnameVerifier).sslSocketFactory(mySSLContext.getSocketFactory()).build();

// default settings for all sockets
IO.setDefaultOkHttpWebSocketFactory(okHttpClient);
IO.setDefaultOkHttpCallFactory(okHttpClient);

// set as an option
IO.Options opts = new IO.Options();
opts.callFactory = okHttpClient;
opts.webSocketFactory = okHttpClient;

socket = IO.socket("https://" + ADDRESS + ":PORT", opts);
socket.connect();

Kotlin 版本:

    val myHostnameVerifier = HostnameVerifier { _, _ ->
        return@HostnameVerifier true
    }

    val trustAllCerts = arrayOf<TrustManager>(object : X509TrustManager
    {
        override fun checkClientTrusted(chain: Array<X509Certificate>, authType: String) {}

        override fun checkServerTrusted(chain: Array<X509Certificate>, authType: String) {}

        override fun getAcceptedIssuers(): Array<X509Certificate> {
            return arrayOf()
        }
    })

    val sslContext = SSLContext.getInstance("TLS")
    sslContext.init(null, trustAllCerts, null)

    val okHttpClient = OkHttpClient.Builder()
                                   .hostnameVerifier(myHostnameVerifier)
                                   .sslSocketFactory(sslContext.socketFactory, trustAllCerts[0] as X509TrustManager)
                                   .build()

    val options = IO.Options()
    options.transports = arrayOf(Polling.NAME)
    options.callFactory = okHttpClient
    options.webSocketFactory = okHttpClient

    val socket = IO.socket("https://...", options)
    socket.connect()