HttpClient 4 – POSTのリダイレクトに従う
1概要
このクイックチュートリアルでは、POSTリクエストのリダイレクトに自動的に従うようにApache HttpClient 4を設定する方法を説明します。
もっと深く掘り下げて、HttpClientを使ってできることを他にも学びたい場合は、
HttpClientのメインチュートリアル
に進んでください。
デフォルトでは、リダイレクトの原因となるGETリクエストのみが自動的に追跡されます。 POSTリクエストが
HTTP 301 Moved Permanently
または
302 Found
–
のいずれかで応答された場合、リダイレクトは自動的には行われません
。
これはhttp://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3[HTTP RFC 2616]で規定されています。
GETまたはHEAD以外の要求に応答して301ステータスコードを受信した場合、要求が発行された条件が変わる可能性があるため、ユーザーエージェントは要求をユーザーが確認できない限り自動的にリダイレクトしてはいけません。
その振る舞いを変更して厳密なHTTP仕様を緩和する必要がある場合は、もちろんユースケースがあります。
まず、デフォルトの動作を確認しましょう。
@Test public void givenPostRequest__whenConsumingUrlWhichRedirects__thenNotRedirected() throws ClientProtocolException, IOException { HttpClient instance = HttpClientBuilder.create().build(); HttpResponse response = instance.execute(new HttpPost("http://t.co/I5YYd9tddw")); assertThat(response.getStatusLine().getStatusCode(), equalTo(301)); }
ご覧のとおり、
リダイレクトの後にデフォルトは続きません
、そして
301ステータスコード
が返されます。===
2 HTTP POST
でリダイレクトする==== ** 2.1. HttpClient 4.3以降の場合
**
HttpClient 4.3では、クライアントの作成と設定の両方に対して、より高度なAPIが導入されました。
@Test public void givenRedirectingPOST__whenConsumingUrlWhichRedirectsWithPOST__thenRedirected() throws ClientProtocolException, IOException { HttpClient instance = HttpClientBuilder.create().setRedirectStrategy(new LaxRedirectStrategy()).build(); HttpResponse response = instance.execute(new HttpPost("http://t.co/I5YYd9tddw")); assertThat(response.getStatusLine().getStatusCode(), equalTo(200)); }
注意
HttpClientBuilder
は、流暢なAPIの出発点になりました。
これにより、以前よりも読みやすい方法でクライアントを完全に設定できます。====
2.2. HttpClient 4.2
の場合以前のバージョンのHttpClient(4.2)では、リダイレクト戦略を直接クライアント上で設定できます。
@SuppressWarnings("deprecation") @Test public void givenRedirectingPOST__whenConsumingUrlWhichRedirectsWithPOST__thenRedirected() throws ClientProtocolException, IOException { DefaultHttpClient client = new DefaultHttpClient(); client.setRedirectStrategy(new LaxRedirectStrategy()); HttpResponse response = client.execute(new HttpPost("http://t.co/I5YYd9tddw")); assertThat(response.getStatusLine().getStatusCode(), equalTo(200)); }
これで、新しい
LaxRedirectStrategy
では、HTTP制限が緩和され、
リダイレクトもPOSTを介して
実行され、
200 OK
ステータスコードになりました。====
2.3. HttpClient 4.2より前のバージョン
HttpClient 4.2より前のバージョンでは、
LaxRedirectStrategy
クラスが存在しなかったため、独自のロールを作成する必要があります。@Test public void givenRedirectingPOST__whenConsumingUrlWhichRedirectsWithPOST__thenRedirected() throws ClientProtocolException, IOException { DefaultHttpClient client = new DefaultHttpClient(); client.setRedirectStrategy(new DefaultRedirectStrategy() { /** ** Redirectable methods. ** / private String[]REDIRECT__METHODS = new String[]{ HttpGet.METHOD__NAME, HttpPost.METHOD__NAME, HttpHead.METHOD__NAME }; @Override protected boolean isRedirectable(String method) { for (String m : REDIRECT__METHODS) { if (m.equalsIgnoreCase(method)) { return true; } } return false; } }); HttpResponse response = client.execute(new HttpPost("http://t.co/I5YYd9tddw")); assertThat(response.getStatusLine().getStatusCode(), equalTo(200)); }
===
3結論
このクイックガイドでは、HTTP POSTリクエストのリダイレクトにも従うようにApache HttpClient 4のどのバージョンを設定するかについても説明しました – 厳密なHTTP標準を緩めます。
これらすべての例とコードスニペットの実装はhttps://github.com/eugenp/tutorials/tree/master/httpclient#readme[my githubプロジェクト]で見つけることができます。** – これはEclipseベースのプロジェクトです。そのままインポートして実行するのは簡単です。