如何将Cookie添加到Undertow的ClientRequest?

时间:2019-04-05 00:17:28

标签: java undertow

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?

1 个答案:

答案 0 :(得分:7)

Cookie存储在请求标头中。因此,您可以这样做:

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){}
}