java 如何在多线程操作中使用 HttpClient?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5455635/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use HttpClient with multithreaded operation?
提问by wangyin
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.
我必须做一个在某个主机中执行登录 POST 请求的应用程序,然后导航一些页面,查找并检索一些数据。因为网站资源是受会话保护的,所以我必须先登录网站,然后才能进行一些操作,例如获取或发布一些数据。我的问题是因为 HttpClient 不是线程安全的,如何只创建一个 HttpClient 实例但线程可以安全地对其执行?请记住,底层连接必须先登录,然后才能使用它进行操作。
回答by nanda
回答by Shamit Verma
You can make HttpClient thread safe by specifying a thread safe client manager.
您可以通过指定线程安全客户端管理器来使 HttpClient 线程安全。
Example : http://thinkandroid.wordpress.com/2009/12/31/creating-an-http-client-example/
示例:http: //thinkandroid.wordpress.com/2009/12/31/creating-an-http-client-example/