HttpClient使用代理


代理伺服器是用戶端和Internet之間的中間伺服器。代理伺服器提供以下基本功能 -

  • 防火牆和網路資料過濾
  • 網路連線共用
  • 資料快取

使用HttpClient庫,可以使用代理傳送HTTP請求。按照下面給出的步驟 -

第1步 - 建立一個HttpHost目標

通過將表示代理主機名稱的字串引數(從中需要傳送請求)傳遞給其建構函式來範例化org.apache.http包的HttpHost類。

//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步 - 將路線規劃器設定為用戶端構建器
使用HttpClients類的custom()方法,建立一個HttpClientBuilder物件,並使用setRoutePlanner()方法為此物件設定上面建立的路徑規劃器。

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

第4步 - 構建CloseableHttpClient物件
通過呼叫build()方法構建CloseableHttpClient物件。

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

第5步 - 建立一個HttpGetobject
通過範例化HttpGet類來建立HTTP GET請求。

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

第6步 - 執行請求
execute()方法的一個變體接受HttpHostHttpRequest物件並執行請求。使用此方法執行請求 -

//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>