I'm using the Tyrus client package to consume, from my Java application, a websocket endpoint that requires a cookie header in the initial client request. Looking through the Tyrus client API docs and Google'ing around hasn't got me too far. Any ideas how one might go about doing this?
Found a solution to my own question, so figured I'd share. The solution is to set a custom configurator on the ClientEndpointConfig and override the beforeRequest method in that configurator to add the cookie header.
For example:
ClientEndpointConfig cec = ClientEndpointConfig.Builder.create()
.configurator(new ClientEndpointConfig.Configurator() {
@Override
public void beforeRequest(Map<String, List<String>> headers) {
super.beforeRequest(headers);
List<String> cookieList = headers.get("Cookie");
if (null == cookieList) {
cookieList = new ArrayList<>();
}
cookieList.add("foo=\"bar\""); // set your cookie value here
headers.put("Cookie", cookieList);
}
}).build();
Then use this ClientEndpointConfig object in your subsequent call to ClientManager.connectToServer or ClientManager.asyncConnectToServer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With