Apache HttpClient 4.2: Keep Proxy AuthCache on the Request Context
Maintain an HttpClient 4.2 proxy Basic-auth cache on the same execution context used by subsequent requests.
In a legacy HttpClient 4.2.2 application, a credentials provider and an authentication cache do different jobs. The provider supplies the account. The cache lets a deliberately preconfigured authentication scheme participate before another challenge arrives.
For the old proxy-specific BasicScheme constructor, the handoff looks like:
AuthCache cache = new BasicAuthCache();
cache.put(proxy, new BasicScheme(ChallengeState.PROXY));
HttpContext context = new BasicHttpContext();
context.setAttribute(ClientContext.AUTH_CACHE, cache);
HttpResponse response = client.execute(request, context);
This fragment assumes client already has the matching proxy route and credentials provider. proxy is its HttpHost, and request is the actual operation. The cache key must describe that intermediary, not the destination website.
Pass the populated context to execute. Building it and then using an overload that creates another context leaves the cache outside the exchange. For later operations intended to share that authentication state, retain the appropriate context rather than reconstructing an empty one each time.
The explicit PROXY state belongs to this older API. TARGET concerns destination authentication. Newer HttpClient generations changed packages and authentication APIs, so use this as maintenance guidance for existing 4.2 code rather than a new-client recipe.
A normal challenge-driven exchange can still be correct. Seed Basic authentication only for a known intended proxy, and consume or release each response before the next operation needs its connection. The useful result is an account and cached scheme attached to the execution that actually sends the request.