Reputation: 4824
I have some code:
public class Foo {
private HttpClient httpClient;
public Foo() {
httpClient = new DefaultHttpClient();
}
}
While chatting with a co-worker (one with a higher level of experience than myself), it came up as a concern that if I create multiple foo()s, that their httpClients might all be impacted by one httpClient's actions. Our concern specifically is Cookies.
If I have code like:
public class Bar {
public static void main(String[] args) {
Foo a = new Foo();
Foo b = new Foo();
a.executeHttpStuff();
}
}
...and executeHttpStuff() utilizes the httpClient, and cookies are added to it, will those cookies be present in any calls made on b?
My hunch is 'no'.
My co-worker's hunch is 'possibly'.
The JavaDoc is not terribly telling.
Do any of you guys know?
Upvotes: 0
Views: 500
Reputation: 3519
HttpClient doesn't shares cookies between instances (via static). So your hunch is right.
You can try it yourself, sniffing traffic from two different instances of client to the same server (via tcpmon e.g.).
Upvotes: 2
Reputation: 9941
The answer is no. Except you take care and have a CookieStore. See: https://hc.apache.org/httpcomponents-client-ga/tutorial/html/statemgmt.html
Have fun.
Upvotes: 1
Reputation: 12561
Per the following documentation page:
http://hc.apache.org/httpclient-3.x/performance.html
"HttpClient is fully thread-safe when used with a thread-safe connection manager such as MultiThreadedHttpConnectionManager"
The javadoc being at:
Upvotes: 2