1

I am testing the Apache HttpAsyncClient, in particular I want to make an asynchronous HTTP POST Request where authentication is needed. I use this example as reference. So far I found out how to set Application type and body but can't find out how to set the credentials.

I try to add Authentication credentials with

    HttpAsyncClientBuilder create = HttpAsyncClientBuilder.create();
    create.setTargetAuthenticationStrategy(new TargetAuthenticationStrategy());
    BasicCredentialsProvider basicCredentialsProvider = new BasicCredentialsProvider();
    Credentials defaultcreds = new UsernamePasswordCredentials("user", "password");
    basicCredentialsProvider.setCredentials(new AuthScope("http://localhost", 7351), defaultcreds);
    create.setDefaultCredentialsProvider(basicCredentialsProvider);

    final CloseableHttpAsyncClient httpclient = create.build();
    httpclient.start();
    ...

But I always get

    Sep 11, 2013 4:21:35 PM org.apache.http.impl.auth.HttpAuthenticator handleAuthChallenge
    WARNING: Malformed challenge: Authentication challenge is empty

I have not found an example which explains how to set authentication data for the CloseableHttpAsyncClient. Anyone can help me out?

ok2c
  • 26,450
  • 5
  • 63
  • 71
user2769795
  • 13
  • 1
  • 4

1 Answers1

0

You can set a credentials provider either at the client level if you want it to be shared by all requests by default

    CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
    CloseableHttpAsyncClient httpclient = HttpAsyncClients.custom()
            .setDefaultCredentialsProvider(credentialsProvider)
            .build();
    httpclient.start();
    try {
        HttpGet request = new HttpGet("http://www.apache.org/");
        Future<HttpResponse> future = httpclient.execute(request, null);
        HttpResponse response = future.get();
        System.out.println("Response: " + response.getStatusLine());
        System.out.println("Shutting down");
    } finally {
        httpclient.close();
    }
    System.out.println("Done");

or set it at the request level, if you want it to apply to a particular request only

    CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
    CloseableHttpAsyncClient httpclient = HttpAsyncClients.createDefault();
    httpclient.start();
    try {
        HttpGet request = new HttpGet("http://www.apache.org/");
        HttpClientContext context = HttpClientContext.create();
        context.setCredentialsProvider(credentialsProvider);
        Future<HttpResponse> future = httpclient.execute(request, context, null);
        HttpResponse response = future.get();
        System.out.println("Response: " + response.getStatusLine());
        System.out.println("Shutting down");
    } finally {
        httpclient.close();
    }
    System.out.println("Done");

Please also note that Malformed challenge: Authentication challenge is empty warning is likely caused by the server sending a malformed (empty) auth challenge rather than HttpClient configuration. Providing user credentials for the request may not necessarily resolve the issue.

ok2c
  • 26,450
  • 5
  • 63
  • 71