A Proxy server is an intermediary server between the client and the internet. Proxy servers offer the following basic functionalities −

  • Firewall and network data filtering

  • Network connection sharing

  • Data caching

Using HttpClient library, you can send a HTTP request using a proxy. Follow the steps given below −

Step 1 - Create a HttpHost object

Instantiate the HttpHost class of the org.apache.http package by passing a string parameter representing the name of the proxy host, (from which you need the requests to be sent) to its constructor.

  1. //Creating an HttpHost object for proxy
  2. HttpHost proxyHost = new HttpHost("localhost");

In the same way, create another HttpHost object to represent the target host to which requests need to be sent.

  1. //Creating an HttpHost object for target
  2. HttpHost targetHost = new HttpHost("google.com");

Step 2 - Create an HttpRoutePlanner object

The HttpRoutePlanner interface computes a route to a specified host. Create an object of this interface by instantiating the DefaultProxyRoutePlanner class, an implementation of this interface. As a parameter to its constructor, pass the above created proxy host −

  1. //creating a RoutePlanner object
  2. HttpRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxyhost);

Step 3 - Set the route planner to a client builder

Using the custom() method of the HttpClients class, create a HttpClientBuilder object and, to this object set the route planner created above, using the setRoutePlanner() method.

  1. //Setting the route planner to the HttpClientBuilder object
  2. HttpClientBuilder clientBuilder = HttpClients.custom();
  3.  
  4. clientBuilder = clientBuilder.setRoutePlanner(routePlanner);

Step 4 - Build the CloseableHttpClient object

Build the CloseableHttpClient object by calling the build() method.

  1. //Building a CloseableHttpClient
  2. CloseableHttpClient httpClient = clientBuilder.build();

Step 5 - Create a HttpGetobject

Create a HTTP GET request by instantiating the HttpGet class.

  1. //Creating an HttpGet object
  2. HttpGet httpGet = new HttpGet("/");

Step 6 - Execute the request

One of the variants of the execute() method accepts an HttpHost and HttpRequest objects and executes the request. Execute the request using this method −

  1. //Executing the Get request
  2. HttpResponse httpResponse = httpclient.execute(targetHost, httpGet);

Example

Following example demonstrates how to send a HTTP request to a server via proxy. In this example, we are sending a HTTP GET request to google.com via localhost. We have printed the headers of the response and the body of the response.

  1. import org.apache.http.Header;
  2. import org.apache.http.HttpEntity;
  3. import org.apache.http.HttpHost;
  4. import org.apache.http.HttpResponse;
  5. import org.apache.http.client.methods.HttpGet;
  6. import org.apache.http.conn.routing.HttpRoutePlanner;
  7. import org.apache.http.impl.client.CloseableHttpClient;
  8. import org.apache.http.impl.client.HttpClientBuilder;
  9. import org.apache.http.impl.client.HttpClients;
  10. import org.apache.http.impl.conn.DefaultProxyRoutePlanner;
  11. import org.apache.http.util.EntityUtils;
  12.  
  13. public class RequestViaProxyExample {
  14.  
  15. public static void main(String args[]) throws Exception{
  16.  
  17. //Creating an HttpHost object for proxy
  18. HttpHost proxyhost = new HttpHost("localhost");
  19.  
  20. //Creating an HttpHost object for target
  21. HttpHost targethost = new HttpHost("google.com");
  22.  
  23. //creating a RoutePlanner object
  24. HttpRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxyhost);
  25.  
  26. //Setting the route planner to the HttpClientBuilder object
  27. HttpClientBuilder clientBuilder = HttpClients.custom();
  28. clientBuilder = clientBuilder.setRoutePlanner(routePlanner);
  29.  
  30. //Building a CloseableHttpClient
  31. CloseableHttpClient httpclient = clientBuilder.build();
  32.  
  33. //Creating an HttpGet object
  34. HttpGet httpget = new HttpGet("/");
  35.  
  36. //Executing the Get request
  37. HttpResponse httpresponse = httpclient.execute(targethost, httpget);
  38.  
  39. //Printing the status line
  40. System.out.println(httpresponse.getStatusLine());
  41.  
  42. //Printing all the headers of the response
  43. Header[] headers = httpresponse.getAllHeaders();
  44.  
  45. for (int i = 0; i < headers.length; i++) {
  46. System.out.println(headers[i]);
  47. }
  48.  
  49. //Printing the body of the response
  50. HttpEntity entity = httpresponse.getEntity();
  51.  
  52. if (entity != null) {
  53. System.out.println(EntityUtils.toString(entity));
  54. }
  55. }
  56. }

Output

On executing, the above program generates the following output −

  1. HTTP/1.1 200 OK
  2. Date: Sun, 23 Dec 2018 10:21:47 GMT
  3. Server: Apache/2.4.9 (Win64) PHP/5.5.13
  4. Last-Modified: Tue, 24 Jun 2014 10:46:24 GMT
  5. ETag: "2e-4fc92abc3c000"
  6. Accept-Ranges: bytes
  7. Content-Length: 46
  8. Content-Type: text/html
  9. <html><body><h1>It works!</h1></body></html>