Set-Cookie
응답 헤더에서 jsessionid
의 압축을 풀어 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 Commons HttpComponents Client을 고려, 적은 비 대한 코드와 동일 달성하기 위해.
확실한 것은 : 폼 기반의 인증을 사용한다면 당신은 자체 개발 한 것이 아니라'j_security_check'를 사용하고 있다고 말할 수 있는가? – BalusC
예 ... j_security_check을 사용하고 있으며 폼 기반 인증을 다시 사용하는 sites.google.com에서 문서를 다운로드하는 코드를 시도했지만 제대로 작동하지 않았습니다. 이 사이트에서만 작동하지 않습니다. 브라우저의 요청은 POST/Site/block/send-receive-updates가됩니다. – user266443