Apache HttpClient - 使用代理

代理服务器是客户端和Internet之间的中间服务器.代理服务器提供以下基本功能 :

  • 防火墙和网络数据过滤

  • 网络连接共享

  • 数据缓存

使用HttpClient库,您可以使用代理发送HTTP请求.按照下面给出的步骤&减去;

步骤1  - 创建一个HttpHost对象

实例化 HttpHost org.apache.http 包通过将表示代理主机名称的字符串参数(从中需要发送请求)传递给其构造函数.

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

以同样的方式,创建另一个HttpHost对象来表示需要发送请求的目标主机.

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

步骤2  - 创建HttpRoutePlanner对象

HttpRoutePlanner 接口计算到指定的主机.通过实例化 DefaultProxyRoutePlanner 类来创建此接口的对象,该类是此接口的实现.作为其构造函数的参数,传递上面创建的代理主机 :

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

步骤3  - 将路线规划器设置为客户端构建器

使用 custom() HttpClients 类的方法,创建一个 HttpClientBuilder 对象,并使用 setRoutePlanner()为此对象设置上面创建的路径规划器方法.

//Setting the route planner to the HttpClientBuilder object
HttpClientBuilder clientBuilder = HttpClients.custom();

clientBuilder = clientBuilder.setRoutePlanner(routePlanner);

步骤4  - 构建CloseableHttpClient对象

通过调用构建 CloseableHttpClient 对象 build()方法.

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

步骤5  - 创建HttpGetobject

通过实例化 HttpGet  class.

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

步骤6  - 执行请求

执行()方法接受 HttpHost HttpRequest 对象并执行请求.使用此方法执行请求 :

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

示例

以下示例演示了如何通过代理向服务器发送HTTP请求.在这个
示例中,我们通过localhost向google.com发送HTTP GET请求.我们打印了响应的标题和响应的主体.

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));
      }
   } 
}

输出

执行时,以上程序生成以下输出 :

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>