如何将Cookie添加到Undertow的ClientRequest?

问题描述 投票:5回答:1
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 Client API的示例。如何在请求中添加cookie?

java undertow
1个回答
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){}
}
© www.soinside.com 2019 - 2024. All rights reserved.