Java websocket client SSL (WSS)

Source: Internet
Author: User

1. How to find a suitable websocket client? See http://en.wikipedia.org/wiki/Comparison_of_WebSocket_implementations

2. Because websocket needs to be used on Android, WSS is required, then I chose java_websocket (http://java-websocket.org /)

This ws client does not support WSS well. The WSS client in the source code also needs to use certificates. My idea is not to verify the certificate. Slightly modify his code:

import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;import java.io.InputStreamReader;import java.net.URI;import java.security.KeyStore;import javax.net.ssl.KeyManagerFactory;import javax.net.ssl.SSLContext;import javax.net.ssl.TrustManagerFactory;import org.java_websocket.WebSocket;import org.java_websocket.client.DefaultSSLWebSocketClientFactory;import org.java_websocket.client.WebSocketClient;import org.java_websocket.handshake.ServerHandshake;class WebSocketChatClient extends WebSocketClient {public WebSocketChatClient( URI serverUri ) {super( serverUri );}@Overridepublic void onOpen( ServerHandshake handshakedata ) {System.out.println( "Connected" );}@Overridepublic void onMessage( String message ) {System.out.println( "got: " + message );}@Overridepublic void onClose( int code, String reason, boolean remote ) {System.out.println( "Disconnected" );System.exit( 0 );}@Overridepublic void onError( Exception ex ) {ex.printStackTrace();}}public class SSLClientExample {

// always verify the host - dont check for certificate    final static HostnameVerifier DO_NOT_VERIFY = new HostnameVerifier() {        public boolean verify(String hostname, SSLSession session) {            return true;        }    };    /**     * Trust every server - dont check for any certificate     */    private static void trustAllHosts(AppWebSocketClient appClient) {        // Create a trust manager that does not validate certificate chains        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 {            }        }};        // Install the all-trusting trust manager        try {            SSLContext sc = SSLContext.getInstance("TLS");            sc.init(null, trustAllCerts, new java.security.SecureRandom());            appClient.setWebSocketFactory(new DefaultSSLWebSocketClientFactory(sc));        } catch (Exception e) {            e.printStackTrace();        }    }public static void main( String[] args ) throws Exception {WebSocket.DEBUG = true;WebSocketChatClient chatclient = new WebSocketChatClient( new URI( "wss://localhost:8887" ) );trustAllHosts(appClient);chatclient.connectBlocking();BufferedReader reader = new BufferedReader( new InputStreamReader( System.in ) );while ( true ) {String line = reader.readLine();if( line.equals( "close" ) ) {chatclient.close();} else {chatclient.send( line );}}}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.