Authenticating a single request with httpclient 4.x

Posted by scompt.com on Stack Overflow See other posts from Stack Overflow or by scompt.com
Published on 2010-03-25T14:33:30Z Indexed on 2010/03/25 15:33 UTC
Read the original article Hit count: 441

I have an HttpClient instance that's shared by a number of threads. I would like to use it to make a single authenticated request. Because only the single request should be authenticated, I don't want to modify the HttpClient instance as described in the documentation. Here's what I've worked out instead, which isn't working. From what I can tell, it doesn't look like the CredentialsProvider is being used at all. Any tips?

HttpContext context = null;
if(feedSpec.isAuthenticated()) {
  context = new BasicHttpContext();
  CredentialsProvider credsProvider = new BasicCredentialsProvider();
  credsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(feedSpec.getHttpUsername(), feedSpec.getHttpPassword()));
  context.setAttribute(ClientContext.CREDS_PROVIDER, credsProvider);
  context.setAttribute(ClientPNames.HANDLE_AUTHENTICATION, true);
}
HttpGet httpGet = new HttpGet(feedSpec.getUri());
HttpResponse httpResponse = httpClient.execute(httpGet, context);

© Stack Overflow or respective owner

Related posts about java

Related posts about authenticate