从登录站点获取HTML

时间:2014-12-25 09:50:56

标签: android html cookies jsoup

我正在开发一款Android应用,我需要从需要登录凭据的网站获取信息。 登录后,它将显示主页,然后单击按钮将其指向数据页面。

我正在获取登录页面的HTML并向其发送用户ID和密码。它很成功,我正在获取主页的HTML代码,现在我正在另外请求有用的数据。

我将cookie信息与URL一起传递,但它正在重定向到登录页面。我错过了我需要传递的任何其他重要信息吗?

// Login code

Connection connection = Jsoup.connect(url).method(Method.GET);
Response response = connection.execute();
                                         cookies = response.cookies();
                                         doc = response.parse();
                                         Element viewstateNode = doc.getElementById("__VIEWSTATE");
                                         Element eventvalidationNode = doc.getElementById("__EVENTVALIDATION");

                                         String viewStateValue = viewstateNode.attr("value");
                                         String eventValidationValue = eventvalidationNode.attr("value");
                                         connection = Jsoup.connect(url)
                                                                   .data("__VIEWSTATE", viewStateValue)
                                                                   .data("__EVENTVALIDATION", eventValidationValue)
                                                                   .data("txtLoginID", username)
                                                                   .data("txtPWD", password)
                                                                   .data("btnLogin", "Go%21")
                                                                   .followRedirects(true)
                                                                   .userAgent("Google")
                                                                   .cookies(cookies)
                                                                   .maxBodySize(100000)
                                                                   .timeout(60000);
                                     response = connection.method(Method.POST).execute();
                                     doc = response.parse();

// Doc contains the HTML code of next page after successful login. Now I need to get the HTML code of next page with useful data.
// I am using the following code to make the second request. 

Connection connection = Jsoup.connect(url_datapage)
                .cookies(cookies)
                .userAgent("Google")
                .maxBodySize(100000)
                .timeout(60000)
                .method(Method.GET);
         Response response = connection.execute();
         doc = response.parse();

但是doc包含登录页面的HTML代码。它再次重定向到主页。

0 个答案:

没有答案