In this chapter, we will learn how to create a HttpRequest authenticated using username and password and tunnel it through a proxy to a target host, using an example.

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.

  1. 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 −

  • AuthScope object − Authentication scope specifying the details like 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.

  1. credsProvider.setCredentials(new AuthScope("example.com", 80), new
  2. UsernamePasswordCredentials("user", "mypass"));
  3. credsProvider.setCredentials(new AuthScope("localhost", 8000), new
  4. UsernamePasswordCredentials("abc", "passwd"));

Step 3 - Create an HttpClientBuilder object

Create a HttpClientBuilder using the custom() method of the HttpClients class as shown below −

  1. //Creating the HttpClientBuilder
  2. HttpClientBuilder clientbuilder = HttpClients.custom();

Step 4 - Set the CredentialsProvider

You can set the CredentialsProvider object to a HttpClientBuilder object using the setDefaultCredentialsProvider() method. Pass the previously created CredentialsProvider object to this method.

  1. clientbuilder = clientbuilder.setDefaultCredentialsProvider(credsProvider);

Step 5 - Build the CloseableHttpClient

Build the CloseableHttpClient object using the build() method.

  1. CloseableHttpClient httpclient = clientbuilder.build();

Step 6 - Create the proxy and target hosts

Create the target and proxy hosts by instantiating the HttpHost class.

  1. //Creating the target and proxy hosts
  2. HttpHost target = new HttpHost("example.com", 80, "http");
  3. HttpHost proxy = new HttpHost("localhost", 8000, "http");

Step 7 - Set the proxy and build a RequestConfig object

Create a RequestConfig.Builder object using the custom() method. Set the previously created proxyHost object to the RequestConfig.Builder using the setProxy() method. Finally, build the RequestConfig object using the build() method.

  1. RequestConfig.Builder reqconfigconbuilder= RequestConfig.custom();
  2. reqconfigconbuilder = reqconfigconbuilder.setProxy(proxyHost);
  3. RequestConfig config = reqconfigconbuilder.build();

Step 8 - Create a HttpGet request object and set config object to it.

Create a HttpGet object by instantiating the HttpGet class. Set the config object created in the previous step to this object using the setConfig() method.

  1. //Create the HttpGet request object
  2. HttpGet httpGet = new HttpGet("/");
  3.  
  4. //Setting the config to the request
  5. httpget.setConfig(config);

Step 9 - Execute the request

Execute the request by passing the HttpHost object (target) and request (HttpGet) as parameters to the execute() method.

  1. HttpResponse httpResponse = httpclient.execute(targetHost, httpget);

Example

Following example demonstrates how to execute a HTTP request through a proxy using username and password.

  1. import org.apache.http.HttpHost;
  2. import org.apache.http.HttpResponse;
  3. import org.apache.http.auth.AuthScope;
  4. import org.apache.http.auth.UsernamePasswordCredentials;
  5. import org.apache.http.client.CredentialsProvider;
  6. import org.apache.http.client.config.RequestConfig;
  7. import org.apache.http.client.methods.HttpGet;
  8. import org.apache.http.impl.client.BasicCredentialsProvider;
  9. import org.apache.http.impl.client.CloseableHttpClient;
  10. import org.apache.http.impl.client.HttpClientBuilder;
  11. import org.apache.http.impl.client.HttpClients;
  12.  
  13. public class ProxyAuthenticationExample {
  14. public static void main(String[] args) throws Exception {
  15.  
  16. //Creating the CredentialsProvider object
  17. CredentialsProvider credsProvider = new BasicCredentialsProvider();
  18.  
  19. //Setting the credentials
  20. credsProvider.setCredentials(new AuthScope("example.com", 80),
  21. new UsernamePasswordCredentials("user", "mypass"));
  22. credsProvider.setCredentials(new AuthScope("localhost", 8000),
  23. new UsernamePasswordCredentials("abc", "passwd"));
  24.  
  25. //Creating the HttpClientBuilder
  26. HttpClientBuilder clientbuilder = HttpClients.custom();
  27.  
  28. //Setting the credentials
  29. clientbuilder = clientbuilder.setDefaultCredentialsProvider(credsProvider);
  30.  
  31. //Building the CloseableHttpClient object
  32. CloseableHttpClient httpclient = clientbuilder.build();
  33.  
  34.  
  35. //Create the target and proxy hosts
  36. HttpHost targetHost = new HttpHost("example.com", 80, "http");
  37. HttpHost proxyHost = new HttpHost("localhost", 8000, "http");
  38.  
  39. //Setting the proxy
  40. RequestConfig.Builder reqconfigconbuilder= RequestConfig.custom();
  41. reqconfigconbuilder = reqconfigconbuilder.setProxy(proxyHost);
  42. RequestConfig config = reqconfigconbuilder.build();
  43.  
  44. //Create the HttpGet request object
  45. HttpGet httpget = new HttpGet("/");
  46.  
  47. //Setting the config to the request
  48. httpget.setConfig(config);
  49.  
  50. //Printing the status line
  51. HttpResponse response = httpclient.execute(targetHost, httpget);
  52. System.out.println(response.getStatusLine());
  53.  
  54. }
  55. }

Output

On executing, the above program generates the following output −

  1. HTTP/1.1 200 OK