如何登录表单(POST)并下载网页源代码? (Java, android)
How to log in to a form (POST) and download the web pages' source? (Java, android)
我需要一个应用程序,你可以提供你的登录凭据,它会为你登录、下载数据并显示它(例如 TextView
)。我可以从源中提取数据等等,但我不知道如何登录等等。 (嗯,我知道我应该通过 post 请求发送凭据,但仅此而已。)
您可以尝试使用 JSOUP 连接并使用凭据登录。它使您能够执行 POST 和 GET 请求。登录后,您可以通过提供链接和 cookie 导航到页面。 挑战在于确保提供成功登录所需的所有数据。
例如:
Connection.Response res = Jsoup.connect("http://some-site.com/login.jsp")
.data("username", "somename")
.data("password", "apass")
.method(Method.POST)
.settimeout(90000)//time set for the connection 1 min
.execute();
Map<String, String> cookies = res.cookies();
Document doc = Jsoup
.connect("http:/some-site.com/home.jsp")
.cookies(cookies)
.get();
然后您可以处理 doc 对象以获得您可能需要的所有信息。
我需要一个应用程序,你可以提供你的登录凭据,它会为你登录、下载数据并显示它(例如 TextView
)。我可以从源中提取数据等等,但我不知道如何登录等等。 (嗯,我知道我应该通过 post 请求发送凭据,但仅此而已。)
您可以尝试使用 JSOUP 连接并使用凭据登录。它使您能够执行 POST 和 GET 请求。登录后,您可以通过提供链接和 cookie 导航到页面。 挑战在于确保提供成功登录所需的所有数据。
例如:
Connection.Response res = Jsoup.connect("http://some-site.com/login.jsp")
.data("username", "somename")
.data("password", "apass")
.method(Method.POST)
.settimeout(90000)//time set for the connection 1 min
.execute();
Map<String, String> cookies = res.cookies();
Document doc = Jsoup
.connect("http:/some-site.com/home.jsp")
.cookies(cookies)
.get();
然后您可以处理 doc 对象以获得您可能需要的所有信息。