当前位置: 首页 > 工具软件 > WSS > 使用案例 >

安卓websocket支持wss

廖弘伟
2023-12-01

websocket支持wss需要在

WebSocketClient.connect();

之前配置

SSLContext sslContext = null;
try {
    sslContext = SSLContext.getInstance("TLS");
} catch (NoSuchAlgorithmException e) {
    e.printStackTrace();
}

try {
    sslContext.init(null, new TrustManager[]{
            new X509TrustManager() {

                @Override
                public void checkClientTrusted(X509Certificate[] chain, String authType) {

                }


                @Override
                public void checkServerTrusted(X509Certificate[] chain, String authType) {

                }

                @Override
                public X509Certificate[] getAcceptedIssuers() {
                    return new X509Certificate[0];
                }
            }
    }, new SecureRandom());
} catch (KeyManagementException e) {
    e.printStackTrace();
}
SSLSocketFactory factory = sslContext.getSocketFactory();

try {
    webSocketClient.setSocket(factory.createSocket());
} catch (IOException e) {
    e.printStackTrace();
}

这样websocket就可以连接wss。

 类似资料: