如何向 Undertow 的 ClientRequest 添加 cookie?

How to add cookies to Undertow's ClientRequest?

final ClientRequest request = new ClientRequest();
request.setMethod(new HttpString(requestMethod));
                    request.getRequestHeaders().put(Headers.TRANSFER_ENCODING, "chunked");
connection.sendRequest(request, new ClientCallback<ClientExchange>() {
    @Override
    public void completed(ClientExchange clientExchange){}
    @Override
    public void failed(IOException e){}
}

我正在遵循 Undertow 客户端的示例 API。如何向请求添加 cookie?

Cookie 存储在请求中 headers。因此你可以这样做:

final ClientRequest request = new ClientRequest();
request.setMethod(new HttpString(requestMethod));
request.getRequestHeaders().put(Headers.TRANSFER_ENCODING, "chunked");
request.getRequestHeaders().put(Headers.COOKIE, "name=value; name2=value2; name3=value3");

connection.sendRequest(request, new ClientCallback<ClientExchange>() {
    @Override
    public void completed(ClientExchange clientExchange){}
    @Override
    public void failed(IOException e){}
}