使用 HttpClient 在不同线程中连接到同一 URL

发布于 2024-08-06 00:27:01 字数 163 浏览 6 评论 0原文

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

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

发布评论

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

评论(4

我为君王 2024-08-13 00:27:01

因为接受的答案仅描述了 HttpClient 3.x 的解决方案,而当前版本是 4.1(这也包含在 Android 中),所以我想分享一个有效的 4.x 示例。也许这可以让某人省去一些麻烦。

SchemeRegistry schemeRegistry = new SchemeRegistry();
schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
schemeRegistry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));

HttpParams parameters = new BasicHttpParams();
ClientConnectionManager connectionManager = new ThreadSafeClientConnManager(parameters, schemeRegistry);
HttpClient httpClient = new DefaultHttpClient(connectionManager, parameters);

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.

SchemeRegistry schemeRegistry = new SchemeRegistry();
schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
schemeRegistry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));

HttpParams parameters = new BasicHttpParams();
ClientConnectionManager connectionManager = new ThreadSafeClientConnManager(parameters, schemeRegistry);
HttpClient httpClient = new DefaultHttpClient(connectionManager, parameters);
俯瞰星空 2024-08-13 00:27:01

我假设您使用 HttpClient 3.0。试试这个,

  HttpClient client = new HttpClient(new MultiThreadedHttpConnectionManager());

I assume you use HttpClient 3.0. Try this,

  HttpClient client = new HttpClient(new MultiThreadedHttpConnectionManager());
作死小能手 2024-08-13 00:27:01

ThreadSafeClientConnManager 也在 4.2 中被弃用。而不是使用 org.apache.http.impl.conn.PoolingHttpClientConnectionManager

ThreadSafeClientConnManager also depricated in 4.2. Instead of use org.apache.http.impl.conn.PoolingHttpClientConnectionManager

乖乖公主 2024-08-13 00:27:01

如果将数据放入应用程序范围,则任何线程都应该可以使用该数据。如果数据敏感,则不应使用此数据,并记住在使用完数据后显式删除它,因为如果不删除,它会在服务器的整个生命周期中存在。

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.

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