”;
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.
//Creating an HttpHost object for proxy 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.
//Creating an HttpHost object for target 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 −
//creating a RoutePlanner object 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.
//Setting the route planner to the HttpClientBuilder object HttpClientBuilder clientBuilder = HttpClients.custom(); clientBuilder = clientBuilder.setRoutePlanner(routePlanner);
Step 4 – Build the CloseableHttpClient object
Build the CloseableHttpClient object by calling the build() method.
//Building a CloseableHttpClient CloseableHttpClient httpClient = clientBuilder.build();
Step 5 – Create a HttpGetobject
Create a HTTP GET request by instantiating the HttpGet class.
//Creating an HttpGet object 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 −
//Executing the Get request 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.
import org.apache.http.Header; import org.apache.http.HttpEntity; import org.apache.http.HttpHost; import org.apache.http.HttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.conn.routing.HttpRoutePlanner; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.impl.client.HttpClients; import org.apache.http.impl.conn.DefaultProxyRoutePlanner; import org.apache.http.util.EntityUtils; public class RequestViaProxyExample { public static void main(String args[]) throws Exception{ //Creating an HttpHost object for proxy HttpHost proxyhost = new HttpHost("localhost"); //Creating an HttpHost object for target HttpHost targethost = new HttpHost("google.com"); //creating a RoutePlanner object HttpRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxyhost); //Setting the route planner to the HttpClientBuilder object HttpClientBuilder clientBuilder = HttpClients.custom(); clientBuilder = clientBuilder.setRoutePlanner(routePlanner); //Building a CloseableHttpClient CloseableHttpClient httpclient = clientBuilder.build(); //Creating an HttpGet object HttpGet httpget = new HttpGet("/"); //Executing the Get request HttpResponse httpresponse = httpclient.execute(targethost, httpget); //Printing the status line System.out.println(httpresponse.getStatusLine()); //Printing all the headers of the response Header[] headers = httpresponse.getAllHeaders(); for (int i = 0; i < headers.length; i++) { System.out.println(headers[i]); } //Printing the body of the response HttpEntity entity = httpresponse.getEntity(); if (entity != null) { System.out.println(EntityUtils.toString(entity)); } } }
Output
On executing, the above program generates the following output −
HTTP/1.1 200 OK Date: Sun, 23 Dec 2018 10:21:47 GMT Server: Apache/2.4.9 (Win64) PHP/5.5.13 Last-Modified: Tue, 24 Jun 2014 10:46:24 GMT ETag: "2e-4fc92abc3c000" Accept-Ranges: bytes Content-Length: 46 Content-Type: text/html <html><body><h1>It works!</h1></body></html>
”;