【问题标题】:Certificate error when using tech.gusavila92.websocketclient.WebSocketClient使用 tech.gusavila92.websocketclient.WebSocketClient 时出现证书错误
【发布时间】:2021-03-27 06:05:43
【问题描述】:

我正在创建一个 Android 应用程序,我想将它连接到运行安全 websockets (wss) 的服务器。我的连接代码如下:

private void createWebSocketClient() {
    URI uri;

    try {
        // Connect to local host
        uri = new URI("wss://mysocketsdomain:8080/ws");
    }

    catch (URISyntaxException e) {
        Log.d("Connection", "I am not even connecting");
        e.printStackTrace();
        return;
    }

    webSocketClient = new WebSocketClient(uri) {
        @Override
        public void onOpen() {
            Log.i("WebSocket", "Session is starting");
            webSocketClient.send("Hello World!");
        }

        @Override
        public void onTextReceived(String s) {
            Log.i("WebSocket", "Message received");
            final String message = s;
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    try{
                        //TextView textView = findViewById(R.id.mainText);
                        //textView.setText(message);
                    } catch (Exception e){
                        e.printStackTrace();
                    }
                }
            });
        }

        @Override
        public void onBinaryReceived(byte[] data) {
        }

        @Override
        public void onPingReceived(byte[] data) {
        }


        @Override
        public void onPongReceived(byte[] data) {
        }
        @Override
        public void onException(Exception e) {
            System.out.println(e.getMessage());
        }
        @Override
        public void onCloseReceived() {
            Log.i("WebSocket", "Closed ");
            System.out.println("onCloseReceived");
        }
    };
    webSocketClient.setConnectTimeout(10000);
    webSocketClient.setReadTimeout(60000);
    webSocketClient.enableAutomaticReconnection(5000);
    webSocketClient.connect();
}

我收到以下错误:

java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.

如果我从浏览器访问 Web 前端,连接工作正常,我可以从挂锁符号看到证书有效。此外,我在 JavaScript 中使用 Svelte 开发的前端 Web 应用程序运行良好。

谁能告诉我需要做什么?我在网上阅读了很多令人困惑的信息。有些事情表明我需要在 Android 应用程序中创建一个受信任的证书存储。我看到的另一件事表明这是服务器证书链的问题。我将服务器设置为使用包含链的证书文件,但错误仍然发生。当它适用于 JavaScript 应用程序和 Web 前端时,我不确定它是否是服务器。

【问题讨论】:

    标签: java android-studio ssl websocket


    【解决方案1】:

    尝试升级 WeSocketClient 库:

        implementation 'tech.gusavila92:java-android-websocket-client:1.2.0'
    

    【讨论】:

    • 抱歉,我不记得九个月前我做了什么来解决这个问题。
    猜你喜欢
    • 2021-11-17
    • 2021-12-14
    • 2017-11-21
    • 1970-01-01
    • 2011-12-27
    • 1970-01-01
    • 2022-01-21
    • 2022-07-01
    • 2021-09-13
    相关资源
    最近更新 更多