Apache-httpclient-using-proxy
提供:Dev Guides
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()メソッドのバリアントの1つは、 *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>