- Multipart Upload
- Custom SSL Context
- Multiple Threads
- Cookies Management
- Form-Based Login
- Proxy Authentication
- Using Proxy
- User Authentication
- Interceptors
- Aborting a Request
- Closing Connection
- Response Handlers
- Http Post Request
- Http Get Request
- Environment Setup
- Overview
- Home
Apache HttpClient Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Apache HttpCpent - User Authentication
Using HttpCpent, you can connect to a website which needed username and password. This chapter explains, how to execute a cpent request against a site that asks for username and password.
Step 1 - Create a CredentialsProvider object
The CredentialsProvider Interface maintains a collection to hold the user login credentials. You can create its object by instantiating the BasicCredentialsProvider class, the default implementation of this interface.
CredentialsProvider credentialsPovider = new BasicCredentialsProvider();
Step 2 - Set the Credentials
You can set the required credentials to the CredentialsProvider object using the setCredentials() method.
This method accepts two objects as given below −
AuthScope object − Authentication scope specifying the details pke hostname, port number, and authentication scheme name.
Credentials object − Specifying the credentials (username, password).
Set the credentials using the setCredentials() method for both host and proxy as shown below −
credsProvider.setCredentials(new AuthScope("example.com", 80), new UsernamePasswordCredentials("user", "mypass")); credsProvider.setCredentials(new AuthScope("localhost", 8000), new UsernamePasswordCredentials("abc", "passwd"));
Step 3 - Create a HttpCpentBuilder Object
Create a HttpCpentBuilder using the custom() method of the HttpCpents class.
//Creating the HttpCpentBuilder HttpCpentBuilder cpentbuilder = HttpCpents.custom();
Step 4 - Set the credentialsPovider
You can set the above created credentialsPovider object to a HttpCpentBuilder using the setDefaultCredentialsProvider() method.
Set the CredentialProvider object created in the previous step to the cpent builder by passing it to the CredentialsProvider object() method as shown below.
cpentbuilder = cpentbuilder.setDefaultCredentialsProvider(credsProvider);
Step 5 - Build the CloseableHttpCpent
Build the CloseableHttpCpent object using the build() method of the HttpCpentBuilder class.
CloseableHttpCpent httpcpent = cpentbuilder.build()
Step 6 - Create a HttpGet object and execute it
Create a HttpRequest object by instantiating the HttpGet class. Execute this request using the execute() method.
//Creating a HttpGet object HttpGet httpget = new HttpGet("https://www.tutorialspoint.com/ "); //Executing the Get request HttpResponse httpresponse = httpcpent.execute(httpget);
Example
Following is an example program which demonstrates the execution of a HTTP request against a target site that requires user authentication.
import org.apache.http.Header; import org.apache.http.HttpResponse; import org.apache.http.auth.AuthScope; import org.apache.http.auth.Credentials; import org.apache.http.auth.UsernamePasswordCredentials; import org.apache.http.cpent.CredentialsProvider; import org.apache.http.cpent.methods.HttpGet; import org.apache.http.impl.cpent.BasicCredentialsProvider; import org.apache.http.impl.cpent.CloseableHttpCpent; import org.apache.http.impl.cpent.HttpCpentBuilder; import org.apache.http.impl.cpent.HttpCpents; pubpc class UserAuthenticationExample { pubpc static void main(String args[]) throws Exception{ //Create an object of credentialsProvider CredentialsProvider credentialsPovider = new BasicCredentialsProvider(); //Set the credentials AuthScope scope = new AuthScope("https://www.tutorialspoint.com/questions/", 80); Credentials credentials = new UsernamePasswordCredentials("USERNAME", "PASSWORD"); credentialsPovider.setCredentials(scope,credentials); //Creating the HttpCpentBuilder HttpCpentBuilder cpentbuilder = HttpCpents.custom(); //Setting the credentials cpentbuilder = cpentbuilder.setDefaultCredentialsProvider(credentialsPovider); //Building the CloseableHttpCpent object CloseableHttpCpent httpcpent = cpentbuilder.build(); //Creating a HttpGet object HttpGet httpget = new HttpGet("https://www.tutorialspoint.com/questions/index.php"); //Printing the method used System.out.println(httpget.getMethod()); //Executing the Get request HttpResponse httpresponse = httpcpent.execute(httpget); //Printing the status pne System.out.println(httpresponse.getStatusLine()); int statusCode = httpresponse.getStatusLine().getStatusCode(); System.out.println(statusCode); Header[] headers= httpresponse.getAllHeaders(); for (int i = 0; i<headers.length;i++) { System.out.println(headers[i].getName()); } } }
Output
On executing, the above program generates the following output.
GET HTTP/1.1 200 OK 200Advertisements