HttpClient 4のPostリダイレクト


概要
この文章はどのようにApache HttpClient 4を配置するかを教えます。自動的にPost要求のリダイレクトに従います。デフォルトでは、GET要求のみがリダイレクトに自動的に従う。1つのPOST要求が返信された状態がHTTP 301またはHTTP 302であれば、自動的にリダイレクトされない。
ここは「web link=」ですhttp://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3"]HTTP RFC 2616[/weblink]の具体的な説明:
If the 301 status code is received in request unest to a request other than GET or HEAD、the user agent Must NOT atomatic redirect the request unless it can confirmed by the user、since this might change the the condiconders the the conders the
私たちはPOSTの301ジャンプを試してみます。まずデフォルトのPOST要求を見てみます。
@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状態コードです。自動的にリダイレクトできませんでした。
HTTP POSTリダイレクト
*2.1 4.3及びより高いhttpClientバージョンは、HttpClient 4.3またはより高いバージョンのAPIでclientの作成と配置を紹介しています。
@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));
}
Http ClintBuiderは今は以前よりもっと使いやすいです。clientの完全な構成を許可します。前よりもっと可読性があります。
2.2 httplient 4.2このバージョンでは、直接clientにリダイレクトルールを配置することができます。
@SuppressWarnings("deprecation")
@Test
public void givenRedirectingPOST_whenConsumingUrlWhichRedirectsWithPOST_thenRedirected() 
  throws ClientProtocolException, IOException {
    DefaultHttpClient client = new DefaultHttpClient();
    client.setRedirectStrategy(new LaxRedirectStrategy());</p>
	HttpResponse response = client.execute(new HttpPost("http://t.co/I5YYd9tddw"));
	assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
}
new LaxRedirect Strategy()を使ってHTTP制限を緩和します。postの時に、方向付けから200に戻る要求状態に自動的に従うことができる。
2.3 HttpClient 4.2以前のバージョンはHttpClient 4.2以前のバージョンの中で、LaxRedictStrategyという種類は存在しないので、私達は自分で実現する必要があります。
@Test
public void givenRedirectingPOST_whenConsumingUrlWhichRedirectsWithPOST_thenRedirected() 
  throws ClientProtocolException, IOException {
    DefaultHttpClient client = new DefaultHttpClient();
    client.setRedirectStrategy(new DefaultRedirectStrategy() {
        /*<em> Redirectable methods. </em>/
        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));
}
結尾語
この記事は、どのようにApache HttpClient 4の様々なバージョンにPOSTのredirectsリダイレクト要求を配置するかを説明しています。問題があれば、メッセージを歓迎します。一緒に討論して解決します。