[英]Connection to same URL in different threads using HttpClient
What is the correct method to get the content of an URL in multiple threads using HttpClient in java? 在Java中使用HttpClient在多个线程中获取URL内容的正确方法是什么?
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. 例如,加载带有项目的列表,同时将每个项目加载到不同的线程中,并使用不同的参数从同一URL获取信息。
In a application I am creating it gives me no element found exception when reading XML from the same URL in different threads.. 在我正在创建的应用程序中,当从不同线程中的同一URL读取XML时,没有发现任何元素异常。
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. 因为接受的答案仅描述了HttpClient 3.x的解决方案,并且当前版本是4.1(Android中也包括此版本),所以我想分享一个有效的4.x示例。 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);
I assume you use HttpClient 3.0. 我假设您使用HttpClient 3.0。 Try this,
尝试这个,
HttpClient client = new HttpClient(new MultiThreadedHttpConnectionManager());
ThreadSafeClientConnManager
also depricated in 4.2. ThreadSafeClientConnManager
在4.2中也有介绍。 Instead of use org.apache.http.impl.conn.PoolingHttpClientConnectionManager
而不是使用
org.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.
如果数据是敏感数据,则不要使用它,并且记住在完成数据处理后将其显式删除,因为如果不删除,它在服务器的整个生命周期中都会存在。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.