简体   繁体   English

使用HttpClient连接到不同线程中的相同URL

[英]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.

相关问题 当请求 url 相同并使用 CompletableFuture 处理时,为什么 Tomcat 使用不同的线程? - Why Tomcat is using different threads when request url is same and handled with CompletableFuture? Java-线程中的URL连接 - Java - URL Connection in Threads 使用httpclient连接持久性 - connection persistence using httpclient java - 通过在java中使用相同的连接,多个线程读取到数据库中的同一个表? - multiple Threads read to the same table in database by using the same connection in java? 使用MultiThreadedHttpConnectionManager为同一httpclient设置不同的套接字超时 - set different socket timeout for same httpclient using MultiThreadedHttpConnectionManager 多个线程如何通过使用Java中的同一连接来写入数据库? - How can multiple Threads write to the database by using the same connection in java? 使用 HttpClient 的 HTTP 连接池 - HTTP connection pooling using HttpClient 使用Apache的HttpClient与使用JDK的URLConnection的applet中的URL连接 - Connection to a URL from within an applet using Apache's HttpClient vs using the JDK's URLConnection 不同的客户端使用相同的连接mysql JSP - Different clients using same connection mysql JSP 可以使用两个线程访问LinkedList中同一对象的不同属性吗? - Possible to access to different attributes of the same object in a LinkedList using two Threads?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM