13

我正在使用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

5 回答 5

14

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

关于该论点的 JavaDocheaders如下所述:

握手请求标头的可变映射实现即将发送以开始握手交互。

那么,为什么不覆盖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)

于 2015-06-30T05:20:42.877 回答
5
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);
    }
}
于 2017-10-10T18:05:59.320 回答
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));
于 2020-02-14T16:17:00.960 回答
1

2020 年更新。这就是 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));

}
于 2020-05-21T03:13:20.690 回答
0

我尝试了如下类似的代码

webSocketContainer.connectToServer(MyClientEndpoint.class, clientConfig, new URI(uri));

这里 'MyClientEndpoint' 扩展了 Endpoint(它应该按照方法规范)。但是 Endpoint 类中没有@OnMessage可用的事件侦听器。如果我只是使用webSocketContainer.connectToServer(this, new URI(uri));,没有客户端配置,我会遇到授权问题。好心提醒。

于 2022-02-03T15:06:12.817 回答