
Apache HttpClient - 使用代理
代理伺服器是客戶端和網際網路之間的中間伺服器。代理伺服器提供以下基本功能:
防火牆和網路資料過濾
網路連線共享
資料快取
使用 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() 方法的其中一個變體接受 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>
廣告