This post originated from an RSS feed registered with Java Buzz
by News Manager.
|
Original Post: HttpClient basic authentication
Feed Title: JavaWorld
Feed URL: http://www.javaworld.com/index.rss
Feed Description: JavaWorld.com: Fueling Innovation
|
Latest Java Buzz Posts
Latest Java Buzz Posts by News Manager
Latest Posts From JavaWorld
|
|
Advertisement
|
1. Overview
This tutorial will illustrate how to configure Basic Authentication on the Apache HttpClient 4.
2. Basic Authentication with the API
Let’s start with the standard way of configuring Basic Authentication on the HttpClient – via a CredentialsProvider:
CredentialsProvider provider = new BasicCredentialsProvider();
UsernamePasswordCredentials credentials = new UsernamePasswordCredentials("user1", "user1Pass");
provider.setCredentials(AuthScope.ANY, credentials);
HttpClient client = HttpClientBuilder.create().setDefaultCredentialsProvider(provider).build();
HttpResponse response = client.execute(new HttpGet(URL_SECURED_BY_BASIC_AUTHENTICATION));
int statusCode = response.getStatusLine().getStatusCode();
assertThat(statusCode, equalTo(HttpStatus.SC_OK));
As you can see, creating the client with a credentials provider to set it up with Basic Authentication is not difficult.
To read this article in full or to leave a comment, please click here
Read: HttpClient basic authentication