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

Java 从使用Icefaces基于表单的身份验证的服务器下载文件,java,jsf,icefaces,Java,Jsf,Icefaces,我是ICEfaces的新手,我需要从给定的url()下载文档 此URL使用通过ICEFaces部署的基于表单的身份验证 我跟踪了此URL的请求,得到以下行: &ice.submit.partial=false&ice.event.target=loginForm%3Aj_id33&ice.event.captured=loginForm%3Aj_id33 是否有任何库或代码可以通过成功传递用户名和密码来下载文档。基于表单的身份验证与其他请求没有太大区别。您所要做的就是向

我是ICEfaces的新手,我需要从给定的url()下载文档

此URL使用通过ICEFaces部署的基于表单的身份验证

我跟踪了此URL的请求,得到以下行:

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

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

基于表单的身份验证与其他请求没有太大区别。您所要做的就是向auth表单提交一个请求,提供所需的参数,例如用户和密码,在某些情况下还需要从源页面获取额外的令牌。然后,您需要从auth response或session id参数中获取Cookie,并将它们复制到下一个将获取数据的请求中。

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

下面是一个借助“纯香草”的启动示例:


用较少的臃肿代码来实现同样的情况,请考虑./P>是肯定的:使用基于表单的身份验证,你的意思是说你使用的是<代码> JSoSurvivsChux<代码>,而不是一个本地的?是的…它使用了j_security_check,我尝试了一个代码从sites.google.com下载一个文档,该文档再次使用了基于表单的身份验证,它起到了作用。仅此网站不起作用。来自浏览器的请求将是POST/site/block/send receive更新

// 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();
// ...