此代碼的工作對我來說:
獲取登錄形式(在我的情況下,當檢索到登錄頁面的cookie被實際設置)
URL url = new URL(formPage);
URLConnection conn = url.openConnection();
String cookieHeader = conn.getHeaderFields().get("Set-Cookie").get(0);
Matcher m = JSESSION_PATTERN.matcher(cookieHeader);
m.find();
jSessionId = m.group(1);
我還需要搶csrf值也是如此,但我現在會省略它。
我那麼做登錄:
URL url = new URL(formPage);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestProperty("Cookie", "JSESSIONID=" + encode(jSessionId, "UTF-8"));
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setDoInput(true);
DataOutputStream out = new DataOutputStream(conn.getOutputStream());
out.writeBytes(String.format("_csrf_token=%s&xyz=%s&zyx=%s",
encode(csrfToken, "UTF-8"),
encode(USER, "UTF-8"),
encode(PASS, "UTF-8")));
out.close();
// I have absolutely no idea why this is needed.
conn.getInputStream().close();
String cookieHeader = conn.getHeaderFields().get("Set-Cookie").get(0);
Matcher m = JSESSION_PATTERN.matcher(cookieHeader);
m.find();
jSessionId = m.group(1);
然後我就可以檢索和使用
URL url = new URL(thePage);
URLConnection conn = url.openConnection();
conn.setRequestProperty("Cookie", "JSESSIONID=" + encode(jSessionId, "UTF-8"));
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String str;
while ((str = in.readLine()) != null) {
// process str...
希望幫助程序頁面!
這個網站的重點是幫助人們解決他們的問題,並公開這樣做,讓其他人可以受益。請張貼您嘗試過的一些代碼,並說明什麼不起作用,並附帶錯誤消息。 – 2010-06-15 18:11:40
這不會有所幫助。我的代碼基本上完全是我鏈接到的網站上的內容。那裏有很多代碼。 我幾天來一直在需要一個答案,任何人都可以幫我嗎? – Cloakbot 2010-06-16 21:18:49
這段鏈接很可能會在一段時間後關閉。但是,該網站會保留您在此處放置的代碼。 – abelito 2012-04-18 12:56:19