My simplified code is:

void makeRequest(final CloseableHttpClient httpClient) {
  URIBuilder uriBuilder = new URIBuilder();
  //Build up URI
  HttpPost httpPost = new HttpPost(uriBuilder.build());
  String body = "...";
  httpPost.setEntity(new StringEntity(body));
  HttpClientContext sessionContext = HttpClientContext.create();
  HttpResponse response = httpClient.execute(httpPost, sessionContext);
}


PoolingHttpClientConnectionManager cm = new 
PoolingHttpClientConnectionManager();
HttpClientBuilder httpClientBuilder = HttpClients.custom();
httpClientBuilder.setConnectionManager(cm);
httpClient = httpClientBuilder.build();

//First Request
makeRequest(httpClient);
//Second Request
makeRequest(httpClient);


Looking at the org.apache.hc.client5.http.headers logs, I can see in the second 
request cookies being sent to the server that were send back by the server in 
the first request.

As I’m creating a brand new HttpClientContext() for each request, surely the 
cookie stores should be separate?

What have I misunderstood?

Thanks,


From: Oleg Kalnichevski <ol...@apache.org>
Date: Saturday, 2 July 2022 at 09:41
To: HttpClient User Discussion <httpclient-users@hc.apache.org>
Subject: Re: Clients, Cookies & Requests
On Fri, 2022-07-01 at 21:29 +0000, Gordon Ross wrote:
> I’m trying to clarify how Cookies, Clients & Requests interact.
>
> I thought that Cookies would only be shared between requests if they
> were saved from a previous request/respone and added to the
> SessionContext passed to the httpclient.execute() method call in a
> subsequent call. But I’m seeing that requests made with separate
> SessionContexts are being made with cookies from other calls.
>
> Is this expected behaviour? Or have I subtly screwed up somewhere?
>
> Thank you,
>

Hi Gordon

No, cookies should not get mixed up if consecutive requests are not
executed within the same execution context. Please note that different
execution contexts can still share the same cookie store.

Oleg


---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscr...@hc.apache.org
For additional commands, e-mail: httpclient-users-h...@hc.apache.org

Reply via email to