Search in sources :

Example 16 with BasicCredentialsProvider

use of org.apache.hc.client5.http.impl.auth.BasicCredentialsProvider in project mercury by yellow013.

the class AsyncClientAuthentication method main.

public static void main(final String[] args) throws Exception {
    final BasicCredentialsProvider credsProvider = new BasicCredentialsProvider();
    credsProvider.setCredentials(new AuthScope("httpbin.org", 80), new UsernamePasswordCredentials("user", "passwd".toCharArray()));
    final CloseableHttpAsyncClient httpclient = HttpAsyncClients.custom().setDefaultCredentialsProvider(credsProvider).build();
    httpclient.start();
    final SimpleHttpRequest request = SimpleRequestBuilder.get("http://httpbin.org/basic-auth/user/passwd").build();
    System.out.println("Executing request " + request);
    final Future<SimpleHttpResponse> future = httpclient.execute(SimpleRequestProducer.create(request), SimpleResponseConsumer.create(), new FutureCallback<SimpleHttpResponse>() {

        @Override
        public void completed(final SimpleHttpResponse response) {
            System.out.println(request + "->" + new StatusLine(response));
            System.out.println(response.getBody());
        }

        @Override
        public void failed(final Exception ex) {
            System.out.println(request + "->" + ex);
        }

        @Override
        public void cancelled() {
            System.out.println(request + " cancelled");
        }
    });
    future.get();
    System.out.println("Shutting down");
    httpclient.close(CloseMode.GRACEFUL);
}
Also used : StatusLine(org.apache.hc.core5.http.message.StatusLine) BasicCredentialsProvider(org.apache.hc.client5.http.impl.auth.BasicCredentialsProvider) AuthScope(org.apache.hc.client5.http.auth.AuthScope) CloseableHttpAsyncClient(org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient) SimpleHttpRequest(org.apache.hc.client5.http.async.methods.SimpleHttpRequest) SimpleHttpResponse(org.apache.hc.client5.http.async.methods.SimpleHttpResponse) UsernamePasswordCredentials(org.apache.hc.client5.http.auth.UsernamePasswordCredentials)

Aggregations

BasicCredentialsProvider (org.apache.hc.client5.http.impl.auth.BasicCredentialsProvider)16 AuthScope (org.apache.hc.client5.http.auth.AuthScope)13 UsernamePasswordCredentials (org.apache.hc.client5.http.auth.UsernamePasswordCredentials)11 CloseableHttpClient (org.apache.hc.client5.http.impl.classic.CloseableHttpClient)8 CloseableHttpResponse (org.apache.hc.client5.http.impl.classic.CloseableHttpResponse)6 HttpHost (org.apache.hc.core5.http.HttpHost)6 HttpGet (org.apache.hc.client5.http.classic.methods.HttpGet)5 URI (java.net.URI)3 Credentials (org.apache.hc.client5.http.auth.Credentials)3 CredentialsStore (org.apache.hc.client5.http.auth.CredentialsStore)3 RequestConfig (org.apache.hc.client5.http.config.RequestConfig)3 SSLContext (javax.net.ssl.SSLContext)2 ServletException (javax.servlet.ServletException)2 AuthCache (org.apache.hc.client5.http.auth.AuthCache)2 CredentialsProvider (org.apache.hc.client5.http.auth.CredentialsProvider)2 BasicAuthCache (org.apache.hc.client5.http.impl.auth.BasicAuthCache)2 HttpClientContext (org.apache.hc.client5.http.protocol.HttpClientContext)2 DefaultConnectionReuseStrategy (org.apache.hc.core5.http.impl.DefaultConnectionReuseStrategy)2 URIBuilder (org.apache.hc.core5.net.URIBuilder)2 Exceptions.throwUnchecked (com.github.tomakehurst.wiremock.common.Exceptions.throwUnchecked)1