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

时间:2016-06-15 05:20:53

标签: android ssl https socket.io tls1.2

在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连接需要什么?

3 个答案:

答案 0 :(得分:7)

感谢@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 {
    }
} };

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

答案 1 :(得分:3)

感谢您@Dayo Choul,我通过https工作获得了socket.io连接。我正在阅读https://github.com/socketio/socket.io-client-java,但它没有告诉您所需的一切。这篇文章仍然缺少这些内容,因此我决定将代码发布给任何可能有用的人。更改地址和端口以适合您的需求。

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

答案 2 :(得分:0)

科特琳版本:

    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()