我需要做任何特别的事情来重新初始化 org.apache.http.client.HttpClient
我有一些代码:
public class Foo {
private HttpClient httpClient;
public Foo() {
httpClient = new DefaultHttpClient();
}
}
在与一位同事(一位比我经验丰富的同事)聊天时,我担心如果我创建多个 foo(),他们的 httpClient 可能都会受到一个 httpClient 的影响行动。我们特别关心的是 Cookie。
如果我有如下代码:
public class Bar {
public static void main(String[] args) {
Foo a = new Foo();
Foo b = new Foo();
a.executeHttpStuff();
}
}
...并且executeHttpStuff() 使用httpClient,并向其中添加cookie,那么这些cookie 是否会出现在b 上进行的任何调用中?
我的预感是“不”。
我同事的预感是“可能”。
JavaDoc 并没有说明什么。
你们有人知道吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
HttpClient 不在实例之间共享 cookie(通过静态)。
所以你的预感是对的。
您可以自己尝试一下,嗅探从客户端的两个不同实例到同一服务器的流量(例如通过 tcpmon)。
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.).
根据以下文档页面:
http://hc.apache.org/httpclient-3 .x/performance.html
“当与线程安全连接管理器(例如 MultiThreadedHttpConnectionManager)一起使用时,HttpClient 是完全线程安全的”
javadoc 位于:
http://hc.apache.org/httpclient -3.x/apidocs/org/apache/commons/httpclient/MultiThreadedHttpConnectionManager.html
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:
http://hc.apache.org/httpclient-3.x/apidocs/org/apache/commons/httpclient/MultiThreadedHttpConnectionManager.html
答案是否定的。除非你小心并有一个 CookieStore。
请参阅:https://hc.apache.org/httpcomponents-client -ga/tutorial/html/statemgmt.html
玩得开心。
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.