0

I am trying to write a java based crawler which authenticates over https and then once authentication completes the client has to invoke an https request to upgrade the connection to websocket

Host: <host> User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:53.0) Gecko/20100101 Firefox/53.0 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 Accept-Language: en-US,en;q=0.5 Accept-Encoding: gzip, deflate, br Sec-WebSocket-Version: 13 Origin: <host> Sec-WebSocket-Extensions: permessage-deflate Sec-WebSocket-Key: VCrrJ5AAhB2cIg/xM2dgAA== Connection: keep-alive, Upgrade Pragma: no-cache Cache-Control: no-cache Upgrade: websocket

I am currently using org.eclipse.jetty.websocket->websocket-client. How can I issue a upgrade request similar to the above using the above library. My application has to be purely java, so I don't mind using any other alternate better java library.

simple
  • 23
  • 3

1 Answers1

0

If you use Tyrus (Tyrus project), you'll find (as stated in the Tyrus Documentation) that you can use the custom configurator for the client and server endpoint. So in your ClientEndpoint annotated instance, you may use as annotation:

@ClientEndpoint(configurator = CustomClientConfigurator.class )
public class WSocketClnt { 
   [...]
}

Where the CustomClientConfigurator is something like:

public class CustomClientConfigurator extends ClientEndpointConfig.Configurator {
    /**
     * This method is called by the implementation after it has formulated the
     * handshake request that will be used to initiate the connection to the
     * server, but before it has sent any part of the request. This allows the
     * developer to inspect and modify the handshake request headers prior to
     * the start of the handshake interaction.
     *
     * @param headers the mutable map of handshake request headers the
     * implementation is about to send to start the handshake interaction.
     */
    @Override
    public void beforeRequest(Map<String, List<String>> headers) {
        headers.get("Connection").add("keep-alive"); // <-- This is only and example

        [...]        
    }

    /**
     * This method is called by the implementation after it has received a
     * handshake response from the server as a result of a handshake interaction
     * it initiated. The developer may implement this method in order to inspect
     * the returning handshake response.
     *
     * @param hr the handshake response sent by the server.
     */
    @Override
    public void afterResponse(HandshakeResponse hr) {
        [...]
    }

}
Andrea Annibali
  • 379
  • 2
  • 5