Apache HTTP 4.x: how to configure the "default context" on HttpClient

In the Apache HTTP Client (4.x, the successor to http 3.x), on the HttpClient method:

 HttpClient.execute(HttpUriRequest request) 

Statuses in JavaDocs:

"Executes the request using the default context."

  • What is the default context (referencing an HttpContext object)?
  • How to set the default context, so I don’t need to pass it on every call to execute() ? (I do not control the execute () call, but I control the creation of the HttpClient)
+4
source share
2 answers

The default context is configured using the HttpClient implementation that you use. For implementations based on AbstractHttpClient , work is done using the createHttpContext() method. Note that for each execute call, a new default context is created.

One way to set the default context is to extend one of the existing HttpClient implementation HttpClient and override the method.

Another way is to set various parameters that the method uses; for example, the connection manager schema registry, authScheme registry, cookieSpecs registry, cookie storage, or credential provider.

For the record here, what DefaultHttpClient.createHttpContext() does:

 @Override protected HttpContext createHttpContext() { HttpContext context = new BasicHttpContext(); context.setAttribute( ClientContext.SCHEME_REGISTRY, getConnectionManager().getSchemeRegistry()); context.setAttribute( ClientContext.AUTHSCHEME_REGISTRY, getAuthSchemes()); context.setAttribute( ClientContext.COOKIESPEC_REGISTRY, getCookieSpecs()); context.setAttribute( ClientContext.COOKIE_STORE, getCookieStore()); context.setAttribute( ClientContext.CREDS_PROVIDER, getCredentialsProvider()); return context; } 
+5
source

From the look at the source code of AbstractHttpClient , which creates the default HttpContext , you can control the values ​​that it is created using the installation attributes in the HttpClient instance, for example, by calling setCredentialsProvider(CredentialsProvider credsProvider) . Is there any specific property (s) that you want to configure?

+1
source

All Articles