HttpClient 4 - カスタムクッキーを送信する

1. 概要

このチュートリアルでは、how to send a Custom Cookie using the Apache HttpClient 4に焦点を当てます。

深く掘り下げて、HttpClientでできる他のクールなことを学びたい場合は、the mainHttpClienttutorialに進んでください。

2.1. 4.3以降のHttpClient

新しいHttpClient4.3では、クライアントの構築と構成の両方を担当する流暢なビルダーAPIを活用します。

まず、Cookieストアを作成し、ストアにサンプルCookieを設定する必要があります。

BasicCookieStore cookieStore = new BasicCookieStore();
BasicClientCookie cookie = new BasicClientCookie("JSESSIONID", "1234");
cookie.setDomain(".github.com");
cookie.setPath("/");
cookieStore.addCookie(cookie);

次に、we can set up this cookie store on the HttpClient using the setDefaultCookieStore() methodを実行して、要求を送信します。

@Test
public void whenSettingCookiesOnTheHttpClient_thenCookieSentCorrectly()
  throws ClientProtocolException, IOException {
    BasicCookieStore cookieStore = new BasicCookieStore();
    BasicClientCookie cookie = new BasicClientCookie("JSESSIONID", "1234");
    cookie.setDomain(".github.com");
    cookie.setPath("/");
    cookieStore.addCookie(cookie);
    HttpClient client = HttpClientBuilder.create().setDefaultCookieStore(cookieStore).build();

    final HttpGet request = new HttpGet("http://www.github.com");

    response = client.execute(request);
    assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
}

非常に重要な要素は、Cookieに設定されているdomainwithout setting the proper domain, the client will not send the cookieです。

2.2. 4.3より前のHttpClient

古いバージョンのHttpClient(4.3より前)では、CookieストアはHttpClientに直接設定されていました。

@Test
public void givenUsingDeprecatedApi_whenSettingCookiesOnTheHttpClient_thenCorrect()
  throws ClientProtocolException, IOException {
    BasicCookieStore cookieStore = new BasicCookieStore();
    BasicClientCookie cookie = new BasicClientCookie("JSESSIONID", "1234");
    cookie.setDomain(".github.com");
    cookie.setPath("/");
    cookieStore.addCookie(cookie);
    DefaultHttpClient client = new DefaultHttpClient();
    client.setCookieStore(cookieStore);

    HttpGet request = new HttpGet("http://www.github.com");

    response = client.execute(request);
    assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
}

クライアントの構築方法を除けば、前の例との違いはありません。

HttpClient全体にCookieを設定するオプションがない場合は、HttpContextクラスを使用してCookieを使用してリクエストを個別に構成できます。

@Test
public void whenSettingCookiesOnTheRequest_thenCookieSentCorrectly()
  throws ClientProtocolException, IOException {
    BasicCookieStore cookieStore = new BasicCookieStore();
    BasicClientCookie cookie = new BasicClientCookie("JSESSIONID", "1234");
    cookie.setDomain(".github.com");
    cookie.setPath("/");
    cookieStore.addCookie(cookie);
    instance = HttpClientBuilder.create().build();

    HttpGet request = new HttpGet("http://www.github.com");

    HttpContext localContext = new BasicHttpContext();
    localContext.setAttribute(HttpClientContext.COOKIE_STORE, cookieStore);
    // localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore); // before 4.3
    response = instance.execute(request, localContext);

    assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
}

HTTPリクエストでCookieを設定する低レベルの代替方法は、生のヘッダーとして設定することです。

@Test
public void whenSettingCookiesOnARequest_thenCorrect()
  throws ClientProtocolException, IOException {
    instance = HttpClientBuilder.create().build();
    HttpGet request = new HttpGet("http://www.github.com");
    request.setHeader("Cookie", "JSESSIONID=1234");

    response = instance.execute(request);

    assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
}

もちろん、これははるかに多くのerror-prone than working with the built in cookie supportです。 たとえば、この場合、ドメインを設定しなくなったことに注意してください。これは正しくありません。

5. 結論

この記事では、work with the HttpClient to send a custom, user controlled Cookieを実行する方法について説明しました。

これは、HttpClientにサーバーによって設定されたCookieを処理させることと同じではないことに注意してください。 代わりに、クライアント側を低レベルで手動で制御します。

これらすべての例とコードスニペットの実装は、my github projectにあります。