How to use Apache HttpClient while the underlying connection is stateful?


I have googled a lot about how to use HttpClient with multithreading. Most of them suggest using the ThreadSafeClientConnManager. But my application has to login some host(a login form page) so that HttpClient obtains a underlying stateful connection. Can ThreadSafeClientConnManager hold the login state if multithreading?


Read the following sections from this page: <a href="http://hc.apache.org/httpcomponents-client-ga/tutorial/html/statemgmt.html#d4e742" rel="nofollow">HttpClient Tutorial about Cookies and state management</a> 3.8. HTTP state management and execution context 3.9. Per user / thread state management

and this maybe that code you want:

HttpClient httpclient = new DefaultHttpClient(); // Create a local instance of cookie store CookieStore cookieStore = new BasicCookieStore(); // Create local HTTP context HttpContext localContext = new BasicHttpContext(); // Bind custom cookie store to the local context localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore); HttpGet httpget = new HttpGet("http://www.google.com/"); // Pass local context as a parameter HttpResponse response = httpclient.execute(httpget, localContext);


Yes, HttpClient will maintain state (E.g. session cookies) with thread safe connection manager.

If you face any issues with login, try switching to "Browser compatibility" cookie policy.



  • Session management in GWT client side
  • Is it possible to generate with Java 8 and Install4j 5.1.5 a warning free installation file
  • Arabic text displaying in webapp without db changes
  • IP and domain create different session
  • Process.StartTime Access Denied
  • Giving security priviliege to a scheduler in Java EE 6
  • Zend Framework 2 - Building a simple form with Validators
  • How to extract a plane from a 3D variable in FiPy (3D to 2D)
  • Selenium and Google - How do you use cookies?
  • How to set up precision attribute used by @Column annotation?
  • In a MEAN stack, how can I do one-time MongoDB indexing?
  • Python PIL remove sections of an image based on its colour
  • How to make a user wait with Laravel
  • Consuming a web service with the Netbeans Platform
  • Command line installation of Code Signing certificates, .p12 files, and mobileprovisions
  • Find 5 consecutive numbers in numpy array by row, ignore duplicates
  • Eclipse MTJ doesn't see Java ME SDK 3.0 devices
  • Redshift Querying: error xx000 disk full redshift
  • Regex to match a string not followed by anything
  • Bash if statement with multiple conditions
  • How to remove a SwiftyJSON element?
  • Swift: Switch statement fallthrough behavior
  • Highlight one bar in a series in highcharts?
  • Lost migrations and Azure database is now out of sync
  • Xamarin Forms - UWP Fonts
  • Is there any way to access browser form field suggestions from JavaScript?
  • Android screen density dpi vs ppi
  • Dynamically accessing properties of knockoutjs observable array
  • JavaScriptCore crash on iOS9
  • script to move all files from one location to another location
  • WinForms: two way TextBox problem
  • Can I make an Android app that runs a web view in Chrome 39?
  • bootstrap to use multiple ng-app
  • How to get icons for entities from eclipse?
  • Turn off referential integrity in Derby? is it possible?
  • Authorize attributes not working in MVC 4
  • JaxB to read class hierarchy
  • Busy indicator not showing up in wpf window [duplicate]
  • Python/Django TangoWithDjango Models and Databases
  • Net Present Value in Excel for Grouped Recurring CF