| 前へ << Java で HTTP クライアントを作ってみよう (2) | 暗号化のお話 (1) >> 次へ |
取得する URL は当ページ
とします。いずれも、ユーザ名は「hoge」パスワードは「fuga」となっています。HttpClientHttpURLConnectionAuth.java
14: URL url = new URL("http://X68000.q-e-d.net/~68user/net/sample/http-auth/secret.html");
15: //URL url = new URL("http://X68000.q-e-d.net/~68user/net/sample/http-auth-digest/secret.html");
16: String username = "hoge";
17: String password = "fuga";
上の URL が Basic 認証のサンプルページ、下の URL が Digest 認証のサンプルページです。
まず、HttpAuthenticator というクラスを定義しておきます。これは Authenticator のサブクラスとします。
61: class HttpAuthenticator extends Authenticator {
62: private String username;
63: private String password;
64: public HttpAuthenticator(String username, String password){
65: this.username = username;
66: this.password = password;
67: }
68: protected PasswordAuthentication getPasswordAuthentication(){
69: return new
70: PasswordAuthentication(username, password.toCharArray());
71: }
72: public String myGetRequestingPrompt(){
73: return super.getRequestingPrompt();
74: }
75: }
19: HttpAuthenticator http_authenticator = new HttpAuthenticator(username, password); 20: Authenticator.setDefault(http_authenticator);
40: System.out.println
41: ("プロンプト(realm)[" + http_authenticator.myGetRequestingPrompt() + "]");
実行結果は以下の通りです。
レスポンスヘッダ: Content-Length: [176] Connection: [Keep-Alive] null: [HTTP/1.1 200 OK] Date: [Sat, 05 Mar 2005 15:41:29 GMT] Keep-Alive: [timeout=15, max=100] Accept-Ranges: [bytes] Server: [Apache/2.0.52 (FreeBSD)] Content-Type: [text/html; charset=euc-jp] レスポンスコード[200] レスポンスメッセージ[OK] プロンプト(realm)[Secret File] ---- ボディ ---- <html> <head> <META HTTP-EQUIV="Content-Type" Content="text/html; charset=EUC-JP"> <title>ひみつのファイル</title> </head> <body> これは秘密ファイルです。 </body> </html>認証に失敗した場合は以下のように例外が発生します。
Exception in thread "main" java.io.IOException
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:591)
at HttpClientHttpURLConnectionAuth.main(HttpClientHttpURLConnectionAuth.java:45)
Caused by: java.net.ProtocolException: Server redirected too many times (20)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:798)
at sun.net.www.protocol.http.HttpURLConnection.getHeaderFields(HttpURLConnection.java:1463)
at HttpClientHttpURLConnectionAuth.main(HttpClientHttpURLConnectionAuth.java:29)
非常に気になるのは「Server redirected too many times (20)」です。
どうやらユーザ名やパスワードが誤っていて認証に失敗した場合、
何度でもリトライをして、最終的にはリトライ回数が 20回に達するときにあきらめてしまうようです。
web サーバのログを見ると、短時間に 19回接続していることがよくわかります。
ある意味、小規模な DoS アタックと言えるでしょう。こんなんでいいんでしょうかね?
(J2SE SDK 1.4.2_07 で確認)
ところで当ページ管理人は
Authenticator authenticator = new Authenticator(username, password);
urlconn.addAuthenticator(authenticator);
System.out.println("Realm[" + authenticator.getRequestingPrompt() + "]");
System.setProperty("http.proxyHost", "proxy.example.com");
System.setProperty("http.proxyPort", "8080");
もし proxy を経由させたくないホストがあれば、上記に加えて
System.setProperty("http.nonProxyHosts", "localhost");
と nonProxyHosts を設定します。
プロパティは java コマンドのオプションとしても指定することができます。
% java -Dhttp.proxyHost=proxy.example.com -Dhttp.proxyPort=8080 \
-Dhttp.nonProxyHosts=localhost ...
| 前へ << Java で HTTP クライアントを作ってみよう (2) | 暗号化のお話 (1) >> 次へ |
ご意見・ご指摘は Twitter: @68user までお願いします。