title: Connect to websocket with self-signed certificate in java
body: <p>I need to use Java to connect to a WebSocket server that is using a self-signed certificate. I'm trying to use the Jetty library and am pretty new at Java but I am finding it very difficult to figure out what needs to be done. I can connect using NodeJS very simply:</p>
  
  <pre><code>const WebSocket = require('ws');
  const ws = new WebSocket('wss://192.168.100.220:9000/', ['ws-valence'], {
    rejectUnauthorized: false,
  });
  </code></pre>
  
  <p>However, modifying the example I found on the <a href="http://www.eclipse.org/jetty/documentation/current/jetty-websocket-client-api.html" rel="nofollow noreferrer">Jetty docs</a> doesn't get me very far. </p>
  
  <p>I implemented a basic client that works well with an echo test server, like in the example linked above. Then I went on to configure it with my own protocol and IP Address:</p>
  
  <pre><code>  private static void connectToBasestation() {
  
  //    String destUri = "ws://echo.websocket.org";
      String basestationUri = "wss://192.168.100.220:9000/";
      SslContextFactory ssl = new SslContextFactory(); // ssl config
      ssl.setTrustAll(true); // trust all certificates
      WebSocketClient client = new WebSocketClient(ssl); // give ssl config to client
      BasestationSocket socket = new BasestationSocket();
      ArrayList&lt;String&gt; protocols = new ArrayList&lt;String&gt;();
      protocols.add("ws-valence");
  
      try
      {
          client.start();
          URI bsUri = new URI(basestationUri);
          ClientUpgradeRequest request = new ClientUpgradeRequest();
          request.setSubProtocols(protocols);
          client.connect(socket, bsUri, request);
          System.out.printf("Connecting to : %s%n", bsUri);
  
          // wait for closed socket connection.
          socket.awaitClose(5,TimeUnit.SECONDS);
      }
      catch (Throwable t)
      {
          t.printStackTrace();
      }
      finally
      {
          try
          {
              client.stop();
          }
          catch (Exception e)
          {
              e.printStackTrace();
  
          }
      }
    }
  </code></pre>
  
  <p>However, I'm getting an <code>UpgradeException</code> with <code>0 null</code> as the values and my <code>onConnect</code> method is never getting called. I'm guessing this is a security issue, but I can't be certain since the server is an old machine -- a bit of a black box. But I'm thinking maybe something is wrong with my approach? Can anyone lend any advice here?</p>
  
  <p>Edit 1: Included trustful SSL factory as suggested. It did not change anything, including the stack trace from below.</p>
  
  <p>Edit 3: There is a similar question listed above, but this is different since 1) I'm getting a different error code and 2) Adding a trustful SSL factory does not solve the issue.</p>
  
  <p>Edit 2: Here is the stack trace I am getting from my OnError below:</p>
  
  <pre><code>Caused by: javax.net.ssl.SSLException: Received fatal alert: handshake_failure
      at sun.security.ssl.Alerts.getSSLException(Alerts.java:208)
      at sun.security.ssl.SSLEngineImpl.fatal(SSLEngineImpl.java:1666)
      at sun.security.ssl.SSLEngineImpl.fatal(SSLEngineImpl.java:1634)
      at sun.security.ssl.SSLEngineImpl.recvAlert(SSLEngineImpl.java:1800)
      at sun.security.ssl.SSLEngineImpl.readRecord(SSLEngineImpl.java:1083)
      at sun.security.ssl.SSLEngineImpl.readNetRecord(SSLEngineImpl.java:907)
      at sun.security.ssl.SSLEngineImpl.unwrap(SSLEngineImpl.java:781)
      at javax.net.ssl.SSLEngine.unwrap(SSLEngine.java:624)
      at org.eclipse.jetty.io.ssl.SslConnection$DecryptedEndPoint.fill(SslConnection.java:681)
      at org.eclipse.jetty.client.http.HttpReceiverOverHTTP.process(HttpReceiverOverHTTP.java:128)
      at org.eclipse.jetty.client.http.HttpReceiverOverHTTP.receive(HttpReceiverOverHTTP.java:73)
      at org.eclipse.jetty.client.http.HttpChannelOverHTTP.receive(HttpChannelOverHTTP.java:133)
      at org.eclipse.jetty.client.http.HttpConnectionOverHTTP.onFillable(HttpConnectionOverHTTP.java:155)
      at org.eclipse.jetty.io.AbstractConnection$ReadCallback.succeeded(AbstractConnection.java:281)
      at org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:102)
      at org.eclipse.jetty.io.ssl.SslConnection.onFillable(SslConnection.java:291)
      at org.eclipse.jetty.io.ssl.SslConnection$3.succeeded(SslConnection.java:151)
      at org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:102)
      at org.eclipse.jetty.io.ChannelEndPoint$2.run(ChannelEndPoint.java:118)
      ... 3 more
  org.eclipse.jetty.websocket.api.UpgradeException: 0 null
      at org.eclipse.jetty.websocket.client.WebSocketUpgradeRequest.onComplete(WebSocketUpgradeRequest.java:522)
      at org.eclipse.jetty.client.ResponseNotifier.notifyComplete(ResponseNotifier.java:216)
      at org.eclipse.jetty.client.ResponseNotifier.notifyComplete(ResponseNotifier.java:208)
      at org.eclipse.jetty.client.HttpReceiver.terminateResponse(HttpReceiver.java:470)
      at org.eclipse.jetty.client.HttpReceiver.abort(HttpReceiver.java:552)
      at org.eclipse.jetty.client.HttpChannel.abortResponse(HttpChannel.java:156)
      at org.eclipse.jetty.client.HttpSender.terminateRequest(HttpSender.java:381)
      at org.eclipse.jetty.client.HttpSender.abort(HttpSender.java:566)
      at org.eclipse.jetty.client.HttpSender.anyToFailure(HttpSender.java:350)
      at org.eclipse.jetty.client.HttpSender$CommitCallback.failed(HttpSender.java:717)
      at org.eclipse.jetty.client.http.HttpSenderOverHTTP$HeadersCallback.failed(HttpSenderOverHTTP.java:310)
      at org.eclipse.jetty.io.WriteFlusher$PendingState.fail(WriteFlusher.java:263)
      at org.eclipse.jetty.io.WriteFlusher.onFail(WriteFlusher.java:516)
      at org.eclipse.jetty.io.ssl.SslConnection$DecryptedEndPoint$FailWrite.run(SslConnection.java:1251)
      at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:762)
      at org.eclipse.jetty.util.thread.QueuedThreadPool$2.run(QueuedThreadPool.java:680)
      at java.lang.Thread.run(Thread.java:748)
  </code></pre>