如何使用HttpClient进行多线程操作?
我必须编写一个应用程序,在某个主机中执行登录 POST 请求,然后导航一些页面,查找并检索一些数据。 由于网站资源受会话保护,因此我必须先登录网站才能进行某些操作,例如获取或发布一些数据。 我的问题是因为 HttpClient 不是线程安全的,我如何才能只创建一个 HttpClient 实例但线程可以安全地执行它? 请记住,底层连接必须先登录才能使用。
I've to do an application that performs a Login POST request in a certain host, then navigates some pages, finds and retrieves some data.
Becase the website resouce is protected by session, so I have to login the website first before I can do some operation such as get or post some data.
My question is because HttpClient is not thread-safe, how can I create only one HttpClient instance but threads can perform on it safely?
Remember that the underlying connection must login first before it can be used to operate.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是一个答案:http://pro-programmers。 blogspot.com/2009/06/apache-httpclient-multi-threads.html
Here is an answer: http://pro-programmers.blogspot.com/2009/06/apache-httpclient-multi-threads.html
您可以通过指定线程安全的客户端管理器来使 HttpClient 线程安全。
API: http ://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/apache/http/impl/conn/tsccm/ThreadSafeClientConnManager.html
http:// /hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/apache/http/impl/client/DefaultHttpClient.html#DefaultHttpClient%28org.apache.http.conn.ClientConnectionManager%29
示例: http://thinkandroid.wordpress.com/2009/ 12/31/创建-http-client-example/
You can make HttpClient thread safe by specifying a thread safe client manager.
API : http://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/apache/http/impl/conn/tsccm/ThreadSafeClientConnManager.html
http://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/apache/http/impl/client/DefaultHttpClient.html#DefaultHttpClient%28org.apache.http.conn.ClientConnectionManager%29
Example : http://thinkandroid.wordpress.com/2009/12/31/creating-an-http-client-example/