如何在Java Websocket客户端中添加标头

时间:2015-06-29 12:02:47

标签: java websocket java-websocket

我使用javax.websocket类连接到Java中的websocket服务器。

import javax.websocket.DeploymentException;
import javax.websocket.Session;
import javax.websocket.WebSocketContainer;
import java.io.IOException;
import java.net.URI;

public class WSClient {
    private WebSocketContainer webSocketContainer;

    public void sendMessage(URI endpointURI, String message) throws IOException, DeploymentException {
        Session session = webSocketContainer.connectToServer(MyClientEndpoint.class, endpointURI);
        session.getAsyncRemote().sendText(message);
    }
}

对于初始HTTP握手,我想在 客户端

上为请求添加额外的HTTP标头

这可能吗?

我知道使用ServerEndpointConfig.Configurator.modifyHandshake可以在服务器端实现这一点。客户端是否有类似的解决方案?

4 个答案:

答案 0 :(得分:9)

ClientEndpointConfig.Configurator.beforeRequest(Map<String,List<String>> headers)可能有用。

关于参数headers的JavaDoc如下所示:

实施即将发送的握手请求标头的 mutable 映射,以启动握手交互。

那么,为什么不覆盖下面的beforeRequest方法?

@Override
public void beforeRequest(Map<String,List<String>> headers)
{
    List<String> values = new ArrayList<String>();
    values.add("My Value");

    headers.put("X-My-Custom-Header", values);
}

您可以将ClientEndpointConfig传递给connectToServer(Class<? extends Endpoint> endpointClass, ClientEndpointConfig cec, URI path)

答案 1 :(得分:2)

Builder configBuilder = ClientEndpointConfig.Builder.create();
configBuilder.configurator(new Configurator() {
    @Override
    public void beforeRequest(Map<String, List<String>> headers) {
    headers.put("Cookie", Arrays.asList("JSESSIONID=" + sessionID));
    }
});
ClientEndpointConfig clientConfig = configBuilder.build();
webSocketContainer.connectToServer(MyClientEndpoint.class, clientConfig, new URI(uri));

答案 2 :(得分:1)

public class Config extends ClientEndpointConfig.Configurator{
    @Override
    public void beforeRequest(Map<String, List<String>> headers) {
        headers.put("Pragma", Arrays.asList("no-cache"));
        headers.put("Origin", Arrays.asList("https://www.bcex.ca"));
        headers.put("Accept-Encoding", Arrays.asList("gzip, deflate, br"));
        headers.put("Accept-Language", Arrays.asList("en-US,en;q=0.8,zh-CN;q=0.6,zh;q=0.4"));
        headers.put("User-Agent", Arrays.asList("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36"));
        headers.put("Upgrade", Arrays.asList("websocket"));
        headers.put("Cache-Control", Arrays.asList("no-cache"));
        headers.put("Connection", Arrays.asList("Upgrade"));
        headers.put("Sec-WebSocket-Version", Arrays.asList("13"));
    }

    @Override
    public void afterResponse(HandshakeResponse hr) {
        Map<String, List<String>> headers = hr.getHeaders();
        log.info("headers -> "+headers);
    }
}

答案 3 :(得分:1)

Update2020。这是IntelliJ社区提出的方式:

  public WsClient(String uri) throws Exception {

    ClientEndpointConfig.Builder configBuilder = ClientEndpointConfig.Builder.create();

    configBuilder.configurator(new ClientEndpointConfig.Configurator() {
        public void beforeRequest(Map<String, List<String>> headers) {
            headers.put("FriendlyName", Arrays.asList("TakaTurautin"));
        }
    });
    ClientEndpointConfig clientConfig = configBuilder.build();
    WebSocketContainer container = ContainerProvider.getWebSocketContainer();

    container.connectToServer(this, clientConfig, URI.create(uri));

}