如何在JavaWebsocket客户端中添加头



我正在使用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的服务器端是可能的。客户端是否有类似的解决方案?

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

关于论点headers的JavaDoc如下所述:

实现要发送的用于启动握手交互的握手请求标头的可变映射

那么,为什么不像下面这样重写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)

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);
    }
}
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。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));
}

我尝试了下面的类似代码

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

此处"MyClientEndpoint"扩展了Endpoint(应符合方法规范)。但是Endpoint类中没有可用的@OnMessage事件侦听器。。如果我简单地使用CCD_ 9;,如果没有客户端配置,我会遇到授权问题。请告知。

相关内容

  • 没有找到相关文章

最新更新