使用 HttpClient 在不同线程中连接到同一 URL
在java中使用HttpClient在多线程中获取URL内容的正确方法是什么?
例如,加载包含项目的列表、同时在不同线程中加载每个项目以及从具有不同参数的同一 URL 获取信息。
在我创建的应用程序中,当我在不同线程中从同一 URL 读取 XML 时,它没有给我发现任何元素异常。
What is the correct method to get the content of an URL in multiple threads using HttpClient in java?
For example loading a List with items, loading each item in a different thread at the same time and getting the information from the same URL with different parameters.
In a application I am creating it gives me no element found exception when reading XML from the same URL in different threads..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
因为接受的答案仅描述了 HttpClient 3.x 的解决方案,而当前版本是 4.1(这也包含在 Android 中),所以我想分享一个有效的 4.x 示例。也许这可以让某人省去一些麻烦。
Because the accepted answer descirbes a solutuion for HttpClient 3.x only, and the current version is 4.1 (This is also included in Android), I would like to share a working 4.x example. Maybe that saves someone some hustle.
我假设您使用 HttpClient 3.0。试试这个,
I assume you use HttpClient 3.0. Try this,
ThreadSafeClientConnManager
也在 4.2 中被弃用。而不是使用 org.apache.http.impl.conn.PoolingHttpClientConnectionManagerThreadSafeClientConnManager
also depricated in 4.2. Instead of useorg.apache.http.impl.conn.PoolingHttpClientConnectionManager
如果将数据放入应用程序范围,则任何线程都应该可以使用该数据。如果数据敏感,则不应使用此数据,并记住在使用完数据后显式删除它,因为如果不删除,它会在服务器的整个生命周期中存在。
If you place the data into application scope it should be available from any thread. You shouldn't use this if the data is sensitive, and remember to explicitly remove it when you are done with it, as it exists through the life of the server if not removed.