RESTEasy (a JAX-RS implementation) has a nice client framework, eg:
ServiceApi client = ProxyFactory.create(ServiceApi.class, baseUri);
How do you provide HTTP authentication credentials to this client?
jnorris's answer uses some deprecated classes. Here is an updated way that uses non-deprecated classes.
import org.apache.http.HttpStatus;
import org.apache.http.auth.Credentials;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.impl.client.DefaultHttpClient;
...
DefaultHttpClient httpClient = new DefaultHttpClient();
Credentials credentials = new UsernamePasswordCredentials(userName,
password);
httpClient.getCredentialsProvider().setCredentials(
org.apache.http.auth.AuthScope.ANY, credentials);
ClientExecutor clientExecutor = new ApacheHttpClient4Executor(
httpClient);
proxy = ProxyFactory
.create(UserAccessProxy.class, host, clientExecutor);
Credentials can be provided by using ClientExecutor.
Credentials credentials = new UsernamePasswordCredentials(userId, password);
HttpClient httpClient = new HttpClient();
httpClient.getState().setCredentials(AuthScope.ANY, credentials);
httpClient.getParams().setAuthenticationPreemptive(true);
ClientExecutor clientExecutor = new ApacheHttpClientExecutor(httpClient);
ServiceApi client = ProxyFactory.create(ServiceApi.class, baseUri, clientExecutor);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With