English 中文(简体)
Proxy Authentication
  • 时间:2024-12-27

Apache HttpCpent - Proxy Authentication


Previous Page Next Page  

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.

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 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 an HttpCpentBuilder object

Create a HttpCpentBuilder using the custom() method of the HttpCpents class as shown below −

//Creating the HttpCpentBuilder
HttpCpentBuilder cpentbuilder = HttpCpents.custom();

Step 4 - Set the CredentialsProvider

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

cpentbuilder = cpentbuilder.setDefaultCredentialsProvider(credsProvider);

Step 5 - Build the CloseableHttpCpent

Build the CloseableHttpCpent object using the build() method.

CloseableHttpCpent httpcpent = cpentbuilder.build();

Step 6 - Create the proxy and target hosts

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

//Creating the target and proxy hosts
HttpHost target = new HttpHost("example.com", 80, "http");
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.

RequestConfig.Builder reqconfigconbuilder= RequestConfig.custom();
reqconfigconbuilder = reqconfigconbuilder.setProxy(proxyHost);
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.

//Create the HttpGet request object
HttpGet httpGet = new HttpGet("/");

//Setting the config to the request
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.

HttpResponse httpResponse = httpcpent.execute(targetHost, httpget);

Example

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

import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.cpent.CredentialsProvider;
import org.apache.http.cpent.config.RequestConfig;
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 ProxyAuthenticationExample {
   pubpc static void main(String[] args) throws Exception {

      //Creating the CredentialsProvider object
      CredentialsProvider credsProvider = new BasicCredentialsProvider();

      //Setting the credentials
      credsProvider.setCredentials(new AuthScope("example.com", 80), 
         new UsernamePasswordCredentials("user", "mypass"));
      credsProvider.setCredentials(new AuthScope("localhost", 8000), 
         new UsernamePasswordCredentials("abc", "passwd"));

      //Creating the HttpCpentBuilder
      HttpCpentBuilder cpentbuilder = HttpCpents.custom();

      //Setting the credentials
      cpentbuilder = cpentbuilder.setDefaultCredentialsProvider(credsProvider);
      
      //Building the CloseableHttpCpent object
      CloseableHttpCpent httpcpent = cpentbuilder.build();


      //Create the target and proxy hosts
      HttpHost targetHost = new HttpHost("example.com", 80, "http");
      HttpHost proxyHost = new HttpHost("localhost", 8000, "http");

      //Setting the proxy
      RequestConfig.Builder reqconfigconbuilder= RequestConfig.custom();
      reqconfigconbuilder = reqconfigconbuilder.setProxy(proxyHost);
      RequestConfig config = reqconfigconbuilder.build();

      //Create the HttpGet request object
      HttpGet httpget = new HttpGet("/");

      //Setting the config to the request
      httpget.setConfig(config);
 
      //Printing the status pne
      HttpResponse response = httpcpent.execute(targetHost, httpget);
      System.out.println(response.getStatusLine());

   }
}

Output

On executing, the above program generates the following output −

HTTP/1.1 200 OK
Advertisements