我需要做任何特别的事情来重新初始化 org.apache.http.client.HttpClient

发布于 2024-12-20 04:58:32 字数 644 浏览 0 评论 0原文

我有一些代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

不醒的梦 2024-12-27 04:58:32

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.).

寻梦旅人 2024-12-27 04:58:32

根据以下文档页面:

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

黯然 2024-12-27 04:58:32

答案是否定的。除非你小心并有一个 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文