0

私は ICEfaces の初心者で、特定の URL ( http://ipaddress/formexec?objectid=201 ) からドキュメントをダウンロードする必要があるという要件があります。

この URL は、ICEFaces を通じて展開されるフォーム ベースの認証を使用します。

この URL のリクエストを追跡したところ、次の行が表示されました。

&ice.submit.partial=false&ice.event.target=loginForm%3Aj_id33&ice.event.captured=loginForm%3Aj_id33

ユーザー名とパスワードを正常に渡してドキュメントをダウンロードするためのライブラリまたはコードはありますか。

4

2 に答える 2

1

jsessionid応答ヘッダーからを抽出し、Set-CookieそれをURL属性として後続のリクエストにとして追加する必要がありますhttp://example.com/path/page.jsf;jsessionid=XXX

これが「プレーンバニラ」の助けを借りたキックオフの例java.net.URLConnectionです:

// Prepare stuff.
String loginurl = "http://example.com/login";
String username = "itsme";
String password = "youneverguess";
URLConnection connection = null;
InputStream response = null;

// First get jsessionid (do as if you're just opening the login page).
connection = new URL(loginurl).openConnection();
response = connection.getInputStream(); // This will actually send the request.
String cookie = connection.getHeaderField("Set-Cookie");
String jsessionid = cookie.split(";")[0].split("=")[1]; // This assumes JSESSIONID is first field (normal case), you may need to change/finetune it.
String jsessionidurl = ";jsessionid=" + jsessionid;
response.close(); // We're only interested in response header. Ignore the response body.

// Now do login.
String authurl = loginurl + "/j_security_check" + jsessionidurl;
connection = new URL(authurl).openConnection();
connection.setDoOutput(true); // Triggers POST method.
PrintWriter writer = new PrintWriter(new OutputStreamWriter(connection.getOutputStream()));
writer.write("j_username=" + URLEncoder.encode(username, "UTF-8")
          + "&j_password=" + URLEncoder.encode(password, "UTF-8"));
writer.close();
response = connection.getInputStream(); // This will actually send the request.
response.close();

// Now you can do any requests in the restricted area using jsessionid. E.g.
String downloadurl = "http://example.com/download/file.ext" + jsessionidurl;
InputStream download = new URL(downloadurl).openStream();
// ...

肥大化の少ないコードで同じことを実現するには、Apache CommonsHttpComponentsClientを検討してください。

于 2010-02-06T02:56:13.137 に答える
0

フォームベースの認証は、他のリクエストと大差ありません。あなたがしなければならないのは、ユーザーとパスワード、場合によってはソースページから取得する必要がある追加のトークンなどの必須パラメーターを提供する認証フォームにリクエストを送信することです。次に、認証応答またはセッション ID パラメーターから Cookie を取得し、データをフェッチする次の要求にそれらをコピーする必要があります。

于 2010-02-04T21:26:14.643 に答える