其他分享
首页 > 其他分享> > Android WebSocket中的SSL连接错误

Android WebSocket中的SSL连接错误

作者:互联网

我开发了一个演示android应用程序,该应用程序通过安全的websocket协议连接到在线服务器.
并且在开始连接时收到“找不到证书路径的信任锚”错误.我搜索了此错误,仅找到了相关的HTTPS,我不知道如何在websocket(wss)中进行开发.

我将Autobahn-SW库用于websocket.

代码在这里(在“我的活动”课程中):

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final WebSocketConnection mConnection = new WebSocketConnection();

    final String wsuri = "wss://myserver_url";
    try {
        mConnection.connect(URI.create(wsuri), new WebSocketConnectionObserver() {

            @Override
            public void onOpen() {
                System.out.println("onOpend----> sending msg...");
                mConnection.sendTextMessage("hello");
            }

            @Override
            public void onClose(WebSocketCloseNotification code, String reason) {
                System.out.println("onClosed---> " + reason);
            }

            @Override
            public void onTextMessage(String payload) {
                System.out.println("onTextmessage---> " + payload);
            }

            @Override
            public void onRawTextMessage(byte[] payload) {
            }

            @Override
            public void onBinaryMessage(byte[] payload) {
            }

        });
    } catch (Exception e) {
        e.printStackTrace();
    }
}

而且我得到如下错误:

07-21 13:16:46.159: D/de.tavendo.autobahn.secure.WebSocketConnection(4023): WebSocket connection created.
07-21 13:16:46.329: 
D/de.tavendo.autobahn.secure.WebSocketReader(4023): WebSocket reader created.
07-21 13:16:46.349: 
D/de.tavendo.autobahn.secure.WebSocketConnection(4023): WebSocket reader created and started.
07-21 13:16:46.349: 
D/de.tavendo.autobahn.secure.WebSocketWriter(4023): WebSocket writer created.
07-21 13:16:46.449: 
E/de.tavendo.autobahn.secure.WebSocketReader(4023): java.security.cert.CertPathValidatorException: Trust anchor for certification path not 
found.
07-21 13:16:46.479: E/de.tavendo.autobahn.secure.WebSocketWriter(4023): Socket is closed
07-21 13:16:46.479: 
D/de.tavendo.autobahn.secure.WebSocketWriter(4023): WebSocker writer running.
07-21 13:16:46.479: 
D/de.tavendo.autobahn.secure.WebSocketConnection(4023): WebSocket writer created and started.
07-21 13:16:46.499: 
D/de.tavendo.autobahn.secure.WebSocketConnection(4023): fail connection [code = INTERNAL_ERROR, reason = WebSockets internal error 
(java.lang.NullPointerException)
07-21 13:16:46.499: D/de.tavendo.autobahn.secure.WebSocketReader(4023): quit
07-21 13:16:46.499: 
D/de.tavendo.autobahn.secure.WebSocketWriter(4023): WebSocket writer ended.
07-21 13:16:46.499: 
D/de.tavendo.autobahn.secure.WebSocketConnection(4023): SocketThread exited.

如何连接Secure Websocket(wss)?代码示例将很有帮助.

解决方法:

感谢@Jack,我解决了以下解决方案:就我而言,我的服务器生成了自我认证证书.但是在服务器获得相关的经过验证的SSL证书后,将不需要(下面)代码.

我也从HTTPS GET (SSL) with Android and self-signed server certificate得到了解决方案.

/*************************************************************************************************/
            /* Below code is only purposed for Testing, Not to use in real environment */
            /**
             * Setting custom Trust managers which are intended to allow SSL connection to server.
             * This custom trust managers are allowing for all connection types, so this may cause network connection security leak.
             * So those are used only for testing purposes.
             *              
             * Doc - http://developer.android.com/training/articles/security-ssl.html#SelfSigned
             * */
            WebSocketClient.setTrustManagers(new TrustManager[] {
              new X509TrustManager() {
                    public void checkClientTrusted(X509Certificate[] chain, String authType) {}
                    public void checkServerTrusted(X509Certificate[] chain, String authType) {}
                    public X509Certificate[] getAcceptedIssuers() { return new X509Certificate[]{}; }
                  }
            });
            /*************************************************************************************************/

            wsClient = new WebSocketClient(uri, this , extraHeaders);       
            wsClient.connect();

标签:websocket,ssl-certificate,android
来源: https://codeday.me/bug/20191121/2052040.html