티스토리 뷰

android native ssl client websocket wss://

안드로이드에서는 bks 사용함.  jks->bks

 


    private void connectWebSocketHttps() throws URISyntaxException, KeyStoreException ,Exception{
        WebSocketChatClient chatclient = new WebSocketChatClient( new URI( "wss://xxx.xxx.kr/examples/websocket/chat" ) );

        // load up the key store
        String STORETYPE = "BKS";
       // String KEYSTORE = "keystore.jks";
        String STOREPASSWORD = "Pass123!";
        String KEYPASSWORD = "Pass123!";

        KeyStore ks = KeyStore.getInstance( STORETYPE );
        //File kf = new File( KEYSTORE );
        ks.load( getResources().openRawResource(R.raw.tomcat7), STOREPASSWORD.toCharArray() );

        KeyManagerFactory kmf = KeyManagerFactory.getInstance( KeyManagerFactory.getDefaultAlgorithm() );
        kmf.init( ks, KEYPASSWORD.toCharArray() );
        TrustManagerFactory tmf = TrustManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
        tmf.init( ks );

        SSLContext sslContext = null;
        sslContext = SSLContext.getInstance( "TLS" );
        sslContext.init( kmf.getKeyManagers(), tmf.getTrustManagers(), null );
        // sslContext.init( null, null, null ); // will use java's default key and trust store which is sufficient unless you deal with self-signed certificates

        SSLSocketFactory factory = sslContext.getSocketFactory();// (SSLSocketFactory) SSLSocketFactory.getDefault();

        chatclient.setSocket( factory.createSocket() );

        chatclient.connectBlocking();

 

      /*
        //exit program.. for java applicatoin
      BufferedReader reader = new BufferedReader( new InputStreamReader( System.in ) );
        while ( true ) {
            String line = reader.readLine();
            if( line!=null&&line.equals( "close" ) ) {
                if(chatclient!=null)
                    chatclient.close();
            } else {
                if(line!=null)
                    chatclient.send( line );
            }
        }*/

    }

댓글