Podstawowa autoryzacja HttpClientBuilder

Od czasu HttpClient 4.3 korzystam z HttpClientBuilder. Łączę się z usługą REST, która ma podstawowe uwierzytelnianie. Ustawiam poświadczenia w następujący sposób:

HttpClientBuilder builder = HttpClientBuilder.create();

// Get the client credentials
String username = Config.get(Constants.CONFIG_USERNAME);
String password = Config.get(Constants.CONFIG_PASSWORD);

// If username and password was found, inject the credentials
if (username != null && password != null)
{
    CredentialsProvider provider = new BasicCredentialsProvider();

    // Create the authentication scope
    AuthScope scope = new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM);

    // Create credential pair
    UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(username, password);

    // Inject the credentials
    provider.setCredentials(scope, credentials);

    // Set the default credentials provider
    builder.setDefaultCredentialsProvider(provider);
}

Jednak to nie działa (usługa REST, której używam, zwraca 401). Co jest nie tak?

questionAnswers(4)

yourAnswerToTheQuestion