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

Apache HttpCpent - Using Proxy


Previous Page Next Page  

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

    Firewall and network data filtering

    Network connection sharing

    Data caching

Using HttpCpent pbrary, 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 cpent builder

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

//Setting the route planner to the HttpCpentBuilder object
HttpCpentBuilder cpentBuilder = HttpCpents.custom();

cpentBuilder = cpentBuilder.setRoutePlanner(routePlanner);

Step 4 - Build the CloseableHttpCpent object

Build the CloseableHttpCpent object by calpng the build() method.

//Building a CloseableHttpCpent
CloseableHttpCpent httpCpent = cpentBuilder.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 = httpcpent.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.cpent.methods.HttpGet;
import org.apache.http.conn.routing.HttpRoutePlanner;
import org.apache.http.impl.cpent.CloseableHttpCpent;
import org.apache.http.impl.cpent.HttpCpentBuilder;
import org.apache.http.impl.cpent.HttpCpents;
import org.apache.http.impl.conn.DefaultProxyRoutePlanner;
import org.apache.http.util.EntityUtils;

pubpc class RequestViaProxyExample {

   pubpc 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 HttpCpentBuilder object
      HttpCpentBuilder cpentBuilder = HttpCpents.custom();
      cpentBuilder = cpentBuilder.setRoutePlanner(routePlanner);

      //Building a CloseableHttpCpent
      CloseableHttpCpent httpcpent = cpentBuilder.build();

      //Creating an HttpGet object
      HttpGet httpget = new HttpGet("/");

      //Executing the Get request
      HttpResponse httpresponse = httpcpent.execute(targethost, httpget);

      //Printing the status pne
      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>
Advertisements