Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Try implementing your own RequestFactory in order to achieve preemptive authentication.</p> <pre><code>public class PreEmptiveAuthHttpRequestFactory extends HttpComponentsClientHttpRequestFactory { public PreEmptiveAuthHttpRequestFactory(DefaultHttpClient client) { super(client); } @Override protected HttpContext createHttpContext(HttpMethod httpMethod, URI uri) { AuthCache authCache = new BasicAuthCache(); BasicScheme basicAuth = new BasicScheme(); HttpHost targetHost = new HttpHost(uri.getHost(), uri.getPort()); authCache.put(targetHost, basicAuth); BasicHttpContext localcontext = new BasicHttpContext(); localcontext.setAttribute(ClientContext.AUTH_CACHE, authCache); return localcontext; } } </code></pre> <p>An then just use it:</p> <pre><code>HttpComponentsClientHttpRequestFactory requestFactory = new PreEmptiveAuthHttpRequestFactory( newHttpClient ); </code></pre> <p>Hope it helps</p> <hr> <p><strong>how to set the username and password</strong> (Copied from @bifur's comment)</p> <p>You can use <code>UserNamePasswordCredentials</code> </p> <pre><code>UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(getUsername(),getPassword()); client.getCredentialsProvider().setCredentials(new AuthScope(getHost(), getPort(), AuthScope.ANY_REALM), credentials); </code></pre> <p>And just use the client in the previous factory </p> <pre><code>HttpComponentsClientHttpRequestFactory requestFactory = new PreEmptiveAuthHttpRequestFactory(client); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload