2014-10-20 107 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