[prev in list] [next in list] [prev in thread] [next in thread] 

List:       httpclient-users
Subject:    Re: Clients, Cookies & Requests
From:       Skylos <skylos () gmail ! com>
Date:       2022-07-03 20:34:32
Message-ID: CADGwUZCy2UjRKPOUAbH02cGGxOF_kQyQJPaK0M6m20P7O2aukw () mail ! gmail ! com
[Download RAW message or body]


You spin up a client.  Where does it get its cookie store?

HttpClientBuilder line 1002 - seventh parameter to Internal HttpClient

Where does it get that?

It copies the cookie store from the cookieStore field in the client Builder
class - or a new BasicCookieStore

Builder only uses anything other than BasicCookieStore unless you call
<builder instance>.setDefaultCookieStore(CookieStore store)

Any requests that are run with the client that you created with this cookie
store are going to use this cookie store, which stores them in a TreeSet in
the fields of the BasicCookieStore object.

SOUNDS LIKE a simple solution to what you want is a
client-per-session-context, rather than
single-client-used-for-multiple-session-contexts.

in the InternalAbstractHttpAsyncClient there is a setupContext method that
says.

if (context.getAttribute(HttpClientContext.COOKIE_STORE) == null) {
    context.setAttribute(HttpClientContext.COOKIE_STORE, cookieStore);
}

so if that's the case, it is going to use the BasicCookieStore in the
client object unless it has something in the HttpClientContext that tells
it to use a different one.

So that would imply if you want your own cookie store per execution context
object, then you're going to have to set it up into that HttpContext.  that
means that the signature you'll have to call execute with needs an
HttpContext - like this one.

    <T> T execute(
            ClassicHttpRequest request,
            HttpContext context,
            HttpClientResponseHandler<? extends T> responseHandler) throws
IOException;

So that'll look something like

HttpClientContext sessionContext = HttpClientContext.create();
sessionContext.setAttribute(HttpClientContext.COOKIE_STORE, new
BasicCookieStore())
client.execute(request, sessionContext, clientResponseHandler);

There's more than one way to do it, of course. Do you want to use one
client with variant HttpClientContext objects... or would you rather have a
client-per-session and use the default cookie stores in the executions?

Hope that's helpful

David




On Fri, Jul 1, 2022 at 5:29 PM Gordon Ross <gr306@cam.ac.uk> 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,
>
>
>
> Gordon Ross (he/him)
> Collaboration Tools Team
> University Information Services
> University of Cambridge
>
> While it suits me to email outside normal working hours, I do not expect a
> response outside your own.
>


-- 
Dog approved this message.


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic