How to use basic authentication in a multi-threaded HTTPClient environment?

I am trying to use HTTPClient to publish a large number of requests to a web service that is protected using basic authentication. Although I use ThreadSafeClientConnManager, the Apache HTTPClient basic authentication implementations do not seem to be thread safe. Sometimes the requesting header does not contain an authorization element that calls 401. Is there a general way or best practice for using basic multi-thread authentication?

+4
source share
2 answers

I recently ran into the same problem. There is this registered problem: https://issues.apache.org/jira/browse/HTTPCLIENT-1168 You have two options:

  • Create an HttpContext for Each Stream or Request
  • Use SyncBasicHttpContext, which is a thread-safe version
+1
source

Are you sure this is a thread safety issue? If you use java.net.Authenticator, it caches names / passwords if you do not:

AuthCacheValue.setAuthCache(new AuthCacheImpl()); 

AuthCacheValue and AuthCacheImpl - from sun.net.www.protocol.http. I do this in a class constructor that extends the authenticator

0
source

Source: https://habr.com/ru/post/1398661/


All Articles