gpt4 book ai didi

java - 从使用 Icefaces 基于表单的身份验证的服务器下载文件

转载 作者:行者123 更新时间:2023-12-01 16:08:12 25 4
gpt4 key购买 nike

我是 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

是否有任何库或代码可以通过成功传递用户名和密码来下载文档。

最佳答案

您需要从 Set-Cookie 响应 header 中提取 jsessionid,并将其作为 URL 属性附加到后续请求中,如 http://example。 com/path/page.jsf;jsessionid=XXX.

这是一个在“plain vanilla”的帮助下的启动示例 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 Commons HttpComponents Client .

关于java - 从使用 Icefaces 基于表单的身份验证的服务器下载文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2202043/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com