”;
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 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.
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 HttpClientBuilder object
Create a HttpClientBuilder using the custom() method of the HttpClients class as shown below −
//Creating the HttpClientBuilder 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.
clientbuilder = clientbuilder.setDefaultCredentialsProvider(credsProvider);
Step 5 – Build the CloseableHttpClient
Build the CloseableHttpClient object using the build() method.
CloseableHttpClient httpclient = clientbuilder.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 = httpclient.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.client.CredentialsProvider; import org.apache.http.client.config.RequestConfig; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.BasicCredentialsProvider; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.impl.client.HttpClients; public class ProxyAuthenticationExample { public 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 HttpClientBuilder HttpClientBuilder clientbuilder = HttpClients.custom(); //Setting the credentials clientbuilder = clientbuilder.setDefaultCredentialsProvider(credsProvider); //Building the CloseableHttpClient object CloseableHttpClient httpclient = clientbuilder.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 line HttpResponse response = httpclient.execute(targetHost, httpget); System.out.println(response.getStatusLine()); } }
Output
On executing, the above program generates the following output −
HTTP/1.1 200 OK
”;