我试图使用java& amp;尝试登录我的帐户访问此网站jsoup。但无论我在这里做了什么改变&在那里,我只是返回登录页面作为回应。我一定有什么问题吗?
我已经使用Firefox的Firebug,Chrome和Fiddler来查看正在发送的值。我已经在jsoup上的StackOverflow上查看了其他一些答案,并且相信我遵循了正确的程序。如果有人能看到我的错误,我会很感激帮助。
String loginURL = "http://members.permaculturedesigncourse.com/login";
String dashURL = "http://members.permaculturedesigncourse.com/dashboard";
String sUserName = "myemail@gmail.com";
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();
}
答案 0 :(得分: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()会在您尝试的时候返回Cookie:
Map<String, String> cookiesFromGet = login_get_res.cookies();
我会尝试使用HttpConnection来获取cookie。像这样:
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变量是否已发送并显示用户内容而不是登录页面。我无法判断您是否已完成此操作,因为我还没有看到您的网络代码。