Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Howto use basic authentication in multi threaded HTTPClient environment?

I am trying to use HTTPClient to post a lot of requests to a webservice which is protected using basic authentication. Although I am using the ThreadSafeClientConnManager, the Basic Authentication implementations of the Apache HTTPClient does not seem to be thread safe. Sometimes the requesting header doesn't contain the authorization element, wich causes a 401. Is there a common way or best practice to use Basic authentication with multiple threads?

like image 499
liecno Avatar asked Nov 04 '22 04:11

liecno


1 Answers

I ran into the same issue recently. There's this logged issue: https://issues.apache.org/jira/browse/HTTPCLIENT-1168 You have two choices it seems:

  1. Create an HttpContext for each thread or request
  2. Use SyncBasicHttpContext which is a thread safe implementation
like image 50
Bogdan Avatar answered Nov 09 '22 12:11

Bogdan