Apache HttpClient - 使用代理

  • 简述

    代理服务器是客户端和互联网之间的中介服务器。代理服务器提供以下基本功能 -
    • 防火墙和网络数据过滤
    • 网络连接共享
    • 数据缓存
    使用 HttpClient 库,您可以使用代理发送 HTTP 请求。按照下面给出的步骤 -
  • 第 1 步 - 创建一个 HttpHost 对象

    通过传递代表代理名称的字符串参数来实例化 org.apache.http 包的 HttpHost 类主机,(您需要从中发送请求)到其构造函数。
    
    //为代理创建一个HttpHost对象
    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 步 - 将路线规划器设置为客户端构建器

    使用HttpClients类的custom()方法,创建一个HttpClientBuilder 对象,并为该对象设置上面创建的路线规划器,使用 setRoutePlanner() 方法。
    
    //Setting the route planner to the HttpClientBuilder object
    HttpClientBuilder clientBuilder = HttpClients.custom();
    clientBuilder = clientBuilder.setRoutePlanner(routePlanner);
    
  • 第 4 步 - 构建 CloseableHttpClient 对象

    通过调用 build() 方法构建 CloseableHttpClient 对象。
    
    //构建一个CloseableHttpClient
    CloseableHttpClient httpClient = clientBuilder.build();
    
  • 第 5 步 - 创建一个 HttpGetobject

    通过实例化 HttpGet 类创建一个 HTTP GET 请求。
    
    //创建一个HttpGet对象
    HttpGet httpGet = new HttpGet("/");
    
  • 第 6 步 - 执行请求

    execute() 方法的变体之一接受 HttpHostHttpRequest 对象并执行请求。使用此方法执行请求 -
    
    //执行Get请求
    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>