2014-10-20 106 views
0

我正在嘗試使用java & jsoup登錄到我的帳戶到此網站。但不管我在這裏做了什麼更改&,我只是返回登錄頁面作爲迴應。我一定有什麼問題?使用Jsoup POST POST登錄和Cookie數據

我已經使用Firefox的Firebug,Chrome和Fiddler來查看正在發送的值。我在jsoup上的StackOverflow上查看了一些其他答案,並相信我遵循正確的過程。如果有人能看到我的錯誤,我會很感激幫助。

String loginURL = "http://members.permaculturedesigncourse.com/login"; 
    String dashURL = "http://members.permaculturedesigncourse.com/dashboard"; 
    String sUserName = "[email protected]"; 
    String sPass = "mypassword"; 

    try { 

     Connection.Response login_get_res = Jsoup 
       .connect(loginURL) 
       .method(Connection.Method.GET) 
       .execute(); 

     Element mySession = login_get_res.parse().getElementById("new_user_session"); 
     String authenticityToken = mySession.select("input[name=authenticity_token]").first().val(); 
     Map<String, String> cookiesFromGet = login_get_res.cookies(); 


     Connection.Response login_post_res = Jsoup 
       .connect(loginURL) 
       .data("authenticity_token", authenticityToken) 
       .data("user_session[email]", sUserName) 
       .data("user_session[password]", sPass) 
       .data("user_session[remember_me]", "0") 
       .data("x", "0") 
       .data("y", "0") 
       .referrer(loginURL) 
       .userAgent("Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.101 Safari/537.36") 
       .cookies(cookiesFromGet) 
       .timeout(12000) 
       .method(Connection.Method.POST) 
       .execute(); 
     Document return_page = login_post_res.parse();  
     String title = return_page.title(); 

    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

回答

1

將所有數據存儲在HashMap命名數據中,並將其作爲數據字段提交。例如, 我會做:

HashMap<String, String> map = new HashMap<String, String>(); 
map.put("userName", userName); 
map.put("password", password); 
map.put("somethingelse", value); 
map.put("anotherthing", anothervalue); 

Document doc = null; 
try { 
    doc = Jsoup.connect(loginUrl).data(map).cookies(cookiesFromGet).post(); 
} catch (IOException e) { 
    e.printStackTrace(); 
} 

此外,我不認爲.cookies()如你想在這裏做的餅乾返回:

Map<String, String> cookiesFromGet = login_get_res.cookies(); 

我會嘗試使用HttpConnection的獲得曲奇餅乾。像這樣:

HttpClient httpClient = new DefaultHttpClient(); 
HttpGet httpget = new HttpGet(loginUrl); 
HttpResponse response = null; 
String responseString = null; 
try { 
    response = httpClient.execute(httpget); 
    responseString = new BasicResponseHandler().handleResponse(response); 
} catch (ClientProtocolException e) { 
    e.printStackTrace(); 
} catch (IOException e) { 
    e.printStackTrace(); 
} 

CookieStore cookieStore = ((AbstractHttpClient) httpClient).getCookieStore(); 
HashMap<String, String> cookies = new HashMap<String, String>(); 
cookies = mapCookies(cookieStore.getCookies()); 

最後,你可能需要做一些更改您的網站代碼,以檢查是否POST變量發送並顯示用戶的內容,而不是登錄頁面。我不知道你是否做過這件事,因爲我沒有看到你的網頁代碼。

+0

我欣賞額外的編輯,但您對AbstractHttpClient的引用已被棄用。我可能已經通過更改爲: – 2014-10-23 19:32:40

+0

我欣賞額外的編輯,但您對AbstractHttpClient的引用已被棄用。我可能已通過更改爲: CookieStore cookieStore =(CookieStore)((AbstractHttpClient)httpClient).getCookieStore(); \t \t 但是你的mapCookie()方法沒有解決。 此外,我可能會誤解,但這不是我嘗試登錄的我的網站。我在另一家公司的網站上有一個帳戶。所以我無法「對您的站點代碼進行一些更改以檢查是否發送了POST變量」 – 2014-10-23 19:35:35