HttpClientでのCookieの扱い(HttpClient3.0-rc3)
HttpClientでは,Cookiの扱いについては,拒否かNetscape仕様かRFC2109かを設定すれば,あとは良きに計らってくれるという嬉しい状況らしいので,確認してみた.
Cookie仕様については,大まかにNetscape仕様かRFC2109かがあるらしい.
たまにこれらの標準仕様では正しく動作しない場合があるらしく,それらに対応するモジュールを組み込む仕組みもあるらしいが,ここではそれには触れず,標準仕様でCookieを扱うための方法とその動作を確認する.
HttpClietnでは,Cookieを扱うためには,HttpMethodオブジェクトにどのCookie仕様に基づいて動作するかのポリシーを設定する.
method.getParams().setCookiePolicy(cookiePolicy);あとは,Cookie仕様に基づいて,サーバからCookieを受け入れるか,サーバにCookieを送信するかどうかが判断され,実際にCookieが送受信される.
本頁で作成したサンプルの動作確認用JSPを以下に示す.
セッションも使用することで,Cookieを使用したセッション管理がサーブレットでなされていても正しく動作できることを確認できる.
Cookieによってセッションが維持されていれば,リクエストの度にCOUNTの値が上がって行く.
また,Cookieがサーバに送信されていれば,サーバ側で受信したCookieが表示される.
では,本体.
HttpClientでは,標準でサポートされているCookieポリシーの識別子が,クラスCookiePolicyの定数にて定義されている.
- CookiePolicy.BROWSER_COMPATIBILITY
- 主要なCookieマネージメントとの高い互換性を維持するモード.
- CookiePolicy.DEFAULT
- デフォルトのCookieポリシー.
デフォルトCookieポリシーは以下で設定できるらしい.
CookiePolicy.registerCookieSpec(CookiePolicy.DEFAULT, <Some CookieSpec>);
CookieSpecは,org.apache.commons.httpclient.cookie.CookieSpecのサブクラスを指定する. - CookiePolicy.IGNORE_COOKIES
- Cookieの使用を拒否する.
- CookiePolicy.NETSCAPE
- Netscape仕様.
- CookiePolicy.RFC_2109
- RFC2109仕様.
method.getParams().setCookiePolicy(cookiePolicy);あとはポリシーに応じて自動的にCookieをサーバとの間でやりとりしてくれるのだが,上位でCookieを取得する方法も用意されている. HttpClientがもつHttpStateから取得できる.
Cookie[] cookies = client.getState().getCookies();
では,サンプルを実行してみる.
まず,Cookieによるセッション維持ができることを確認してみる.
$> java -jar httpclientsample.jar 0 3 http://thor.fireproject.jp/fireproject/cookiedisplay.jsp cookiePolicy = compatibility 2005-08-08 21:52:16,328 DEBUG [main] header Wire 69 - >> "GET /fireproject/cookiedisplay.jsp HTTP/1.1[\r][\n]" 2005-08-08 21:52:16,356 DEBUG [main] header Wire 69 - >> "User-Agent: Jakarta Commons-HttpClient/3.0-rc3[\r][\n]" 2005-08-08 21:52:16,365 DEBUG [main] header Wire 69 - >> "Host: thor.fireproject.jp[\r][\n]" 2005-08-08 21:52:16,366 DEBUG [main] header Wire 69 - >> "[\r][\n]" 2005-08-08 21:52:16,382 DEBUG [main] header Wire 69 - << "HTTP/1.1 200 OK[\r][\n]" 2005-08-08 21:52:16,386 DEBUG [main] header Wire 69 - << "Date: Mon, 08 Aug 2005 12:52:16 GMT[\r][\n]" 2005-08-08 21:52:16,387 DEBUG [main] header Wire 69 - << "Server: Apache/1.3.33 (Debian GNU/Linux) mod_ssl/2.8.22 OpenSSL/0.9.7d mod_jk/1.2.5[\r][\n]" 2005-08-08 21:52:16,388 DEBUG [main] header Wire 69 - << "Set-Cookie: JSESSIONID=4BDA864D69FCC93A8B600C38C3290ADB; Path=/fireproject[\r][\n]" 2005-08-08 21:52:16,389 DEBUG [main] header Wire 69 - << "Set-Cookie: HOGENAME=HOGEVALUE; Domain=.fireproject.jp; Expires=Mon, 08-Aug-2005 13:02:16 GMT; Path=/fireproject[\r][\n]" 2005-08-08 21:52:16,389 DEBUG [main] header Wire 69 - << "Content-Length: 52[\r][\n]" 2005-08-08 21:52:16,391 DEBUG [main] header Wire 69 - << "Content-Type: text/html;;charset=ISO-8859-1[\r][\n]" ===== 2 Cookies ===== JSESSIONID=4BDA864D69FCC93A8B600C38C3290ADB / JSESSIONID / 4BDA864D69FCC93A8B600C38C3290ADB / thor.fireproject.jp / 0 / null / /fireproject / null / false HOGENAME=HOGEVALUE / HOGENAME / HOGEVALUE / .fireproject.jp / 0 / Mon Aug 08 22:02:16 JST 2005 / /fireproject / null / false <html> <body> COUNT = 0 <hr> </body> </html> 2005-08-08 21:52:16,502 DEBUG [main] header Wire 69 - >> "GET /fireproject/cookiedisplay.jsp HTTP/1.1[\r][\n]" 2005-08-08 21:52:16,505 DEBUG [main] header Wire 69 - >> "User-Agent: Jakarta Commons-HttpClient/3.0-rc3[\r][\n]" 2005-08-08 21:52:16,506 DEBUG [main] header Wire 69 - >> "Host: thor.fireproject.jp[\r][\n]" 2005-08-08 21:52:16,507 DEBUG [main] header Wire 69 - >> "Cookie: JSESSIONID=4BDA864D69FCC93A8B600C38C3290ADB[\r][\n]" 2005-08-08 21:52:16,508 DEBUG [main] header Wire 69 - >> "Cookie: HOGENAME=HOGEVALUE[\r][\n]" 2005-08-08 21:52:16,508 DEBUG [main] header Wire 69 - >> "[\r][\n]" 2005-08-08 21:52:16,515 DEBUG [main] header Wire 69 - << "HTTP/1.1 200 OK[\r][\n]" 2005-08-08 21:52:16,517 DEBUG [main] header Wire 69 - << "Date: Mon, 08 Aug 2005 12:52:16 GMT[\r][\n]" 2005-08-08 21:52:16,517 DEBUG [main] header Wire 69 - << "Server: Apache/1.3.33 (Debian GNU/Linux) mod_ssl/2.8.22 OpenSSL/0.9.7d mod_jk/1.2.5[\r][\n]" 2005-08-08 21:52:16,524 DEBUG [main] header Wire 69 - << "Set-Cookie: HOGENAME=HOGEVALUE; Domain=.fireproject.jp; Expires=Mon, 08-Aug-2005 13:02:16 GMT; Path=/fireproject[\r][\n]" 2005-08-08 21:52:16,525 DEBUG [main] header Wire 69 - << "Content-Length: 491[\r][\n]" 2005-08-08 21:52:16,526 DEBUG [main] header Wire 69 - << "Content-Type: text/html;;charset=ISO-8859-1[\r][\n]" ===== 2 Cookies ===== JSESSIONID=4BDA864D69FCC93A8B600C38C3290ADB / JSESSIONID / 4BDA864D69FCC93A8B600C38C3290ADB / thor.fireproject.jp / 0 / null / /fireproject / null / false HOGENAME=HOGEVALUE / HOGENAME / HOGEVALUE / .fireproject.jp / 0 / Mon Aug 08 22:02:16 JST 2005 / /fireproject / null / false <html> <body> COUNT = 1 <hr> cookie name = JSESSIONID<br> cookie value = 4BDA864D69FCC93A8B600C38C3290ADB<br> cookie domain = null<br> cookie version = 0<br> cookie maxAge = -1<br> cookie Path = null<br> cookie Comment = null<br> cookie Secure = false<br> <hr> cookie name = HOGENAME<br> cookie value = HOGEVALUE<br> cookie domain = null<br> cookie version = 0<br> cookie maxAge = -1<br> cookie Path = null<br> cookie Comment = null<br> cookie Secure = false<br> <hr> </body> </html> 2005-08-08 21:52:16,534 DEBUG [main] header Wire 69 - >> "GET /fireproject/cookiedisplay.jsp HTTP/1.1[\r][\n]" 2005-08-08 21:52:16,536 DEBUG [main] header Wire 69 - >> "User-Agent: Jakarta Commons-HttpClient/3.0-rc3[\r][\n]" 2005-08-08 21:52:16,538 DEBUG [main] header Wire 69 - >> "Host: thor.fireproject.jp[\r][\n]" 2005-08-08 21:52:16,541 DEBUG [main] header Wire 69 - >> "Cookie: JSESSIONID=4BDA864D69FCC93A8B600C38C3290ADB[\r][\n]" 2005-08-08 21:52:16,543 DEBUG [main] header Wire 69 - >> "Cookie: HOGENAME=HOGEVALUE[\r][\n]" 2005-08-08 21:52:16,544 DEBUG [main] header Wire 69 - >> "[\r][\n]" 2005-08-08 21:52:16,553 DEBUG [main] header Wire 69 - << "HTTP/1.1 200 OK[\r][\n]" 2005-08-08 21:52:16,554 DEBUG [main] header Wire 69 - << "Date: Mon, 08 Aug 2005 12:52:16 GMT[\r][\n]" 2005-08-08 21:52:16,555 DEBUG [main] header Wire 69 - << "Server: Apache/1.3.33 (Debian GNU/Linux) mod_ssl/2.8.22 OpenSSL/0.9.7d mod_jk/1.2.5[\r][\n]" 2005-08-08 21:52:16,557 DEBUG [main] header Wire 69 - << "Set-Cookie: HOGENAME=HOGEVALUE; Domain=.fireproject.jp; Expires=Mon, 08-Aug-2005 13:02:16 GMT; Path=/fireproject[\r][\n]" 2005-08-08 21:52:16,563 DEBUG [main] header Wire 69 - << "Content-Length: 491[\r][\n]" 2005-08-08 21:52:16,564 DEBUG [main] header Wire 69 - << "Content-Type: text/html;;charset=ISO-8859-1[\r][\n]" ===== 2 Cookies ===== JSESSIONID=4BDA864D69FCC93A8B600C38C3290ADB / JSESSIONID / 4BDA864D69FCC93A8B600C38C3290ADB / thor.fireproject.jp / 0 / null / /fireproject / null / false HOGENAME=HOGEVALUE / HOGENAME / HOGEVALUE / .fireproject.jp / 0 / Mon Aug 08 22:02:16 JST 2005 / /fireproject / null / false <html> <body> COUNT = 2 <hr> cookie name = JSESSIONID<br> cookie value = 4BDA864D69FCC93A8B600C38C3290ADB<br> cookie domain = null<br> cookie version = 0<br> cookie maxAge = -1<br> cookie Path = null<br> cookie Comment = null<br> cookie Secure = false<br> <hr> cookie name = HOGENAME<br> cookie value = HOGEVALUE<br> cookie domain = null<br> cookie version = 0<br> cookie maxAge = -1<br> cookie Path = null<br> cookie Comment = null<br> cookie Secure = false<br> <hr> </body> </html>Cookieが送受信されていること,セッション維持のためのJSESSIONIDが正しく送受信されているので,セッション維持ができ,COUNTの値が上がって行くことを確認できる.次に,ポリシーとしてCookieを拒否する設定をしてみる.
$> java -jar httpclientsample.jar 2 2 http://thor.fireproject.jp/fireproject/cookiedisplay.jsp cookiePolicy = ignoreCookies 2005-08-08 21:57:03,519 DEBUG [main] header Wire 69 - >> "GET /fireproject/cookiedisplay.jsp HTTP/1.1[\r][\n]" 2005-08-08 21:57:03,548 DEBUG [main] header Wire 69 - >> "User-Agent: Jakarta Commons-HttpClient/3.0-rc3[\r][\n]" 2005-08-08 21:57:03,557 DEBUG [main] header Wire 69 - >> "Host: thor.fireproject.jp[\r][\n]" 2005-08-08 21:57:03,558 DEBUG [main] header Wire 69 - >> "[\r][\n]" 2005-08-08 21:57:03,570 DEBUG [main] header Wire 69 - << "HTTP/1.1 200 OK[\r][\n]" 2005-08-08 21:57:03,574 DEBUG [main] header Wire 69 - << "Date: Mon, 08 Aug 2005 12:57:03 GMT[\r][\n]" 2005-08-08 21:57:03,575 DEBUG [main] header Wire 69 - << "Server: Apache/1.3.33 (Debian GNU/Linux) mod_ssl/2.8.22 OpenSSL/0.9.7d mod_jk/1.2.5[\r][\n]" 2005-08-08 21:57:03,576 DEBUG [main] header Wire 69 - << "Set-Cookie: JSESSIONID=F1F2A0E66A9AAC9B9CE3BF22B590F495; Path=/fireproject[\r][\n]" 2005-08-08 21:57:03,577 DEBUG [main] header Wire 69 - << "Set-Cookie: HOGENAME=HOGEVALUE; Domain=.fireproject.jp; Expires=Mon, 08-Aug-2005 13:07:03 GMT; Path=/fireproject[\r][\n]" 2005-08-08 21:57:03,578 DEBUG [main] header Wire 69 - << "Content-Length: 52[\r][\n]" 2005-08-08 21:57:03,578 DEBUG [main] header Wire 69 - << "Content-Type: text/html;;charset=ISO-8859-1[\r][\n]" ===== 0 Cookies ===== <html> <body> COUNT = 0 <hr> </body> </html> 2005-08-08 21:57:03,596 DEBUG [main] header Wire 69 - >> "GET /fireproject/cookiedisplay.jsp HTTP/1.1[\r][\n]" 2005-08-08 21:57:03,598 DEBUG [main] header Wire 69 - >> "User-Agent: Jakarta Commons-HttpClient/3.0-rc3[\r][\n]" 2005-08-08 21:57:03,601 DEBUG [main] header Wire 69 - >> "Host: thor.fireproject.jp[\r][\n]" 2005-08-08 21:57:03,602 DEBUG [main] header Wire 69 - >> "[\r][\n]" 2005-08-08 21:57:03,610 DEBUG [main] header Wire 69 - << "HTTP/1.1 200 OK[\r][\n]" 2005-08-08 21:57:03,612 DEBUG [main] header Wire 69 - << "Date: Mon, 08 Aug 2005 12:57:03 GMT[\r][\n]" 2005-08-08 21:57:03,612 DEBUG [main] header Wire 69 - << "Server: Apache/1.3.33 (Debian GNU/Linux) mod_ssl/2.8.22 OpenSSL/0.9.7d mod_jk/1.2.5[\r][\n]" 2005-08-08 21:57:03,613 DEBUG [main] header Wire 69 - << "Set-Cookie: JSESSIONID=6DC4257B5DFBBBB05B49A5B9F0BE6641; Path=/fireproject[\r][\n]" 2005-08-08 21:57:03,614 DEBUG [main] header Wire 69 - << "Set-Cookie: HOGENAME=HOGEVALUE; Domain=.fireproject.jp; Expires=Mon, 08-Aug-2005 13:07:03 GMT; Path=/fireproject[\r][\n]" 2005-08-08 21:57:03,615 DEBUG [main] header Wire 69 - << "Content-Length: 52[\r][\n]" 2005-08-08 21:57:03,617 DEBUG [main] header Wire 69 - << "Content-Type: text/html;;charset=ISO-8859-1[\r][\n]" ===== 0 Cookies ===== <html> <body> COUNT = 0 <hr> </body> </html>サーバからCookieが送信されてきているが,クライアントでは無視し,サーバに送信もしないことが確認できる.最後に,Domain外のサーバにはCookieを送信しないことを確認する.
$> java -jar httpclientsample.jar 0 1 http://thor.fireproject.jp/fireproject/cookiedisplay.jsp\ http://www.example.com/ http://thor.fireproject.jp/fireproject/cookiedisplay.jsp cookiePolicy = compatibility 2005-08-08 21:58:54,710 DEBUG [main] header Wire 69 - >> "GET /fireproject/cookiedisplay.jsp HTTP/1.1[\r][\n]" 2005-08-08 21:58:54,740 DEBUG [main] header Wire 69 - >> "User-Agent: Jakarta Commons-HttpClient/3.0-rc3[\r][\n]" 2005-08-08 21:58:54,750 DEBUG [main] header Wire 69 - >> "Host: thor.fireproject.jp[\r][\n]" 2005-08-08 21:58:54,751 DEBUG [main] header Wire 69 - >> "[\r][\n]" 2005-08-08 21:58:54,766 DEBUG [main] header Wire 69 - << "HTTP/1.1 200 OK[\r][\n]" 2005-08-08 21:58:54,770 DEBUG [main] header Wire 69 - << "Date: Mon, 08 Aug 2005 12:58:54 GMT[\r][\n]" 2005-08-08 21:58:54,770 DEBUG [main] header Wire 69 - << "Server: Apache/1.3.33 (Debian GNU/Linux) mod_ssl/2.8.22 OpenSSL/0.9.7d mod_jk/1.2.5[\r][\n]" 2005-08-08 21:58:54,771 DEBUG [main] header Wire 69 - << "Set-Cookie: JSESSIONID=62ADE4EC2699314AED7F40CD937AF9B4; Path=/fireproject[\r][\n]" 2005-08-08 21:58:54,772 DEBUG [main] header Wire 69 - << "Set-Cookie: HOGENAME=HOGEVALUE; Domain=.fireproject.jp; Expires=Mon, 08-Aug-2005 13:08:54 GMT; Path=/fireproject[\r][\n]" 2005-08-08 21:58:54,772 DEBUG [main] header Wire 69 - << "Content-Length: 52[\r][\n]" 2005-08-08 21:58:54,773 DEBUG [main] header Wire 69 - << "Content-Type: text/html;;charset=ISO-8859-1[\r][\n]" ===== 2 Cookies ===== JSESSIONID=62ADE4EC2699314AED7F40CD937AF9B4 / JSESSIONID / 62ADE4EC2699314AED7F40CD937AF9B4 / thor.fireproject.jp / 0 / null / /fireproject / null / false HOGENAME=HOGEVALUE / HOGENAME / HOGEVALUE / .fireproject.jp / 0 / Mon Aug 08 22:08:54 JST 2005 / /fireproject / null / false <html> <body> COUNT = 0 <hr> </body> </html> 2005-08-08 21:58:55,023 DEBUG [main] header Wire 69 - >> "GET / HTTP/1.1[\r][\n]" 2005-08-08 21:58:55,024 DEBUG [main] header Wire 69 - >> "User-Agent: Jakarta Commons-HttpClient/3.0-rc3[\r][\n]" 2005-08-08 21:58:55,025 DEBUG [main] header Wire 69 - >> "Host: www.example.com[\r][\n]" 2005-08-08 21:58:55,026 DEBUG [main] header Wire 69 - >> "[\r][\n]" 2005-08-08 21:58:55,192 DEBUG [main] header Wire 69 - << "HTTP/1.1 200 OK[\r][\n]" 2005-08-08 21:58:55,193 DEBUG [main] header Wire 69 - << "Date: Mon, 08 Aug 2005 12:58:56 GMT[\r][\n]" 2005-08-08 21:58:55,194 DEBUG [main] header Wire 69 - << "Server: Apache/1.3.27 (Unix) (Red-Hat/Linux)[\r][\n]" 2005-08-08 21:58:55,194 DEBUG [main] header Wire 69 - << "Last-Modified: Wed, 08 Jan 2003 23:11:55 GMT[\r][\n]" 2005-08-08 21:58:55,195 DEBUG [main] header Wire 69 - << "ETag: "3f80f-1b6-3e1cb03b"[\r][\n]" 2005-08-08 21:58:55,197 DEBUG [main] header Wire 69 - << "Accept-Ranges: bytes[\r][\n]" 2005-08-08 21:58:55,202 DEBUG [main] header Wire 69 - << "Content-Length: 438[\r][\n]" 2005-08-08 21:58:55,203 DEBUG [main] header Wire 69 - << "Connection: close[\r][\n]" 2005-08-08 21:58:55,204 DEBUG [main] header Wire 69 - << "Content-Type: text/html[\r][\n]" ===== 2 Cookies ===== JSESSIONID=62ADE4EC2699314AED7F40CD937AF9B4 / JSESSIONID / 62ADE4EC2699314AED7F40CD937AF9B4 / thor.fireproject.jp / 0 / null / /fireproject / null / false HOGENAME=HOGEVALUE / HOGENAME / HOGEVALUE / .fireproject.jp / 0 / Mon Aug 08 22:08:54 JST 2005 / /fireproject / null / false <HTML> <HEAD> <TITLE>Example Web Page</TITLE> </HEAD> <body> <p>You have reached this web page by typing "example.com", "example.net", or "example.org" into your web browser.</p> <p>These domain names are reserved for use in documentation and are not available for registration. See <a href="http://www.rfc-editor.org/rfc/rfc2606.txt">RFC 2606</a>, Section 3.</p> </BODY> </HTML> 2005-08-08 21:58:55,209 DEBUG [main] header Wire 69 - >> "GET /fireproject/cookiedisplay.jsp HTTP/1.1[\r][\n]" 2005-08-08 21:58:55,213 DEBUG [main] header Wire 69 - >> "User-Agent: Jakarta Commons-HttpClient/3.0-rc3[\r][\n]" 2005-08-08 21:58:55,216 DEBUG [main] header Wire 69 - >> "Host: thor.fireproject.jp[\r][\n]" 2005-08-08 21:58:55,218 DEBUG [main] header Wire 69 - >> "Cookie: JSESSIONID=62ADE4EC2699314AED7F40CD937AF9B4[\r][\n]" 2005-08-08 21:58:55,219 DEBUG [main] header Wire 69 - >> "Cookie: HOGENAME=HOGEVALUE[\r][\n]" 2005-08-08 21:58:55,220 DEBUG [main] header Wire 69 - >> "[\r][\n]" 2005-08-08 21:58:55,233 DEBUG [main] header Wire 69 - << "HTTP/1.1 200 OK[\r][\n]" 2005-08-08 21:58:55,234 DEBUG [main] header Wire 69 - << "Date: Mon, 08 Aug 2005 12:58:55 GMT[\r][\n]" 2005-08-08 21:58:55,235 DEBUG [main] header Wire 69 - << "Server: Apache/1.3.33 (Debian GNU/Linux) mod_ssl/2.8.22 OpenSSL/0.9.7d mod_jk/1.2.5[\r][\n]" 2005-08-08 21:58:55,236 DEBUG [main] header Wire 69 - << "Set-Cookie: HOGENAME=HOGEVALUE; Domain=.fireproject.jp; Expires=Mon, 08-Aug-2005 13:08:55 GMT; Path=/fireproject[\r][\n]" 2005-08-08 21:58:55,239 DEBUG [main] header Wire 69 - << "Content-Length: 491[\r][\n]" 2005-08-08 21:58:55,240 DEBUG [main] header Wire 69 - << "Content-Type: text/html;;charset=ISO-8859-1[\r][\n]" ===== 2 Cookies ===== JSESSIONID=62ADE4EC2699314AED7F40CD937AF9B4 / JSESSIONID / 62ADE4EC2699314AED7F40CD937AF9B4 / thor.fireproject.jp / 0 / null / /fireproject / null / false HOGENAME=HOGEVALUE / HOGENAME / HOGEVALUE / .fireproject.jp / 0 / Mon Aug 08 22:08:55 JST 2005 / /fireproject / null / false <html> <body> COUNT = 1 <hr> cookie name = JSESSIONID<br> cookie value = 62ADE4EC2699314AED7F40CD937AF9B4<br> cookie domain = null<br> cookie version = 0<br> cookie maxAge = -1<br> cookie Path = null<br> cookie Comment = null<br> cookie Secure = false<br> <hr> cookie name = HOGENAME<br> cookie value = HOGEVALUE<br> cookie domain = null<br> cookie version = 0<br> cookie maxAge = -1<br> cookie Path = null<br> cookie Comment = null<br> cookie Secure = false<br> <hr> </body> </html>まず,thor.fireproject.jpからCookieを受け入れ,保持するが,二回目のGETはサーバがwww.example.comなので,受け入れたCookieは送信していない. そして三回目で再びthor.fireproject.jpに送信する際にはCookieを送信し,Count値からセッションを維持できていることを確認できた.

