HTTPクライアントライブラリの概要
現代のアプリケーション開発において、HTTPプロトコルを介したリソース取得は不可欠な要素です。Java標準のjava.netパッケージでは機能が限られるため、Apache Commons HttpClientのような専用ライブラリが広く採用されています。このライブラリは最新のHTTP/1.1およびHTTP/2仕様をサポートし、企業向けシステムからスモールスケールアプリケーションまで幅広く利用可能です。
主要機能の実装パターン
基本的なHTTPメソッド操作からセキュア通信まで、以下の実装パターンを解説します。
GETリクエストの実装
リソース取得にはHttpGetクラスを使用します。接続管理を自動化するため、try-with-resources構文を活用した安全な実装を推奨します。
import org.apache.hc.client5.http.classic.methods.HttpGet;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.apache.hc.core5.http.io.entity.EntityUtils;
public class HttpGetter {
public static void main(String[] args) {
final String targetUrl = "https://example.com/resource";
try (CloseableHttpClient client = HttpClients.createDefault()) {
HttpGet request = new HttpGet(targetUrl);
request.setConfig(
RequestConfig.custom()
.setConnectTimeout(5000)
.build()
);
try (CloseableHttpResponse response = client.execute(request)) {
if (response.getCode() == 200) {
String content = EntityUtils.toString(response.getEntity());
System.out.println("取得成功: " + content.substring(0, 100) + "...");
} else {
System.err.println("ステータス異常: " + response.getCode());
}
}
} catch (IOException e) {
System.err.println("ネットワークエラー: " + e.getMessage());
}
}
}
フォーム送信の実装
POSTリクエストではUrlEncodedFormEntityを使用してフォームデータを構築します。リダイレクト処理はデフォルトで有効ですが、カスタマイズが可能です。
import org.apache.hc.client5.http.entity.UrlEncodedFormEntity;
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
import org.apache.hc.core5.http.message.BasicNameValuePair;
public class FormPoster {
public static void postCredentials(String url, String user, String pass) {
try (CloseableHttpClient client = HttpClients.createDefault()) {
HttpPost request = new HttpPost(url);
// フォームデータの構築
List<NameValuePair> params = new ArrayList<>();
params.add(new BasicNameValuePair("username", user));
params.add(new BasicNameValuePair("password", pass));
request.setEntity(new UrlEncodedFormEntity(params));
try (CloseableHttpResponse response = client.execute(request)) {
// リダイレクト処理(302時)
if (response.getCode() == 302) {
Header location = response.getFirstHeader("Location");
if (location != null) {
String redirectUrl = location.getValue();
System.out.println("リダイレクト先: " + redirectUrl);
}
}
}
} catch (Exception e) {
// エラーハンドリング
}
}
}
実装上の重要なポイント
文字エンコーディングの制御
レスポンスの文字コードは以下の手順で処理すべきです:
- HTTPヘッダーの
Content-Typeからcharsetを抽出 - HTMLの
<meta charset>タグを解析 - 両者を比較し、信頼性の高い方を採用
エンティティ取得時は明示的に文字コードを指定:
String content = EntityUtils.toString(
response.getEntity(),
ContentType.getOrDefault(response.getEntity()).getCharset()
);
SSL/TLS通信の設定
証明書検証を無視するカスタムSSLContextの実装例:
public static CloseableHttpClient createInsecureClient() {
try {
SSLContext sslContext = SSLContextBuilder
.create()
.loadTrustMaterial((chain, authType) -> true)
.build();
return HttpClients.custom()
.setSSLContext(sslContext)
.setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE)
.build();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
※本番環境では証明書検証を無効化しないでください
プロキシ設定の適用
プロキシ経由での通信にはHttpHostオブジェクトを使用:
HttpHost proxy = new HttpHost("proxy.example.com", 8080);
RequestConfig config = RequestConfig.custom()
.setProxy(proxy)
.build();
HttpGet request = new HttpGet("https://target.com");
request.setConfig(config);