我想使用 Java 在网页上自动登录。我已经使用 Jsoup 设置了用户 ID,但不知道如何将该数据发送到浏览器。
最佳答案
您可以使用jsoup登录网页。代码如下所示:
Connection.Response loginForm = Jsoup.connect("https://login.to/")
.ignoreContentType(true)
.userAgent("Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:25.0) Gecko/20100101 Firefox/25.0")
.referrer("http://www.google.com")
.timeout(12000)
.followRedirects(true)
.method(Connection.Method.GET)
.execute();
Connection.Response loginFormFilled = Jsoup.connect("https://login.to/")
.ignoreContentType(true)
.userAgent("Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:25.0) Gecko/20100101 Firefox/25.0")
.followRedirects(true)
.referrer("https://login.to/")
.data("LOGON", "user")//check the form to find field name for user name
.data("PASSWORDS", "pass")//check the form to find field name for user password
.cookies(loginForm.cookies())
.method(Connection.Method.POST)
.execute();
int statusCode = loginFormFilled.statusCode();
Map<String, String> cookies = loginFormFilled.cookies();
您应该在登录时检查发送到服务器的内容,以便填充第二个请求的数据。之后检查响应代码是否为200,即表示成功。您还可以检查 cookie,应该有 JSESSIONID
或类似的内容表明您有一个 session ,这是与登录用户交互的常见方式。
关于java - 需要使用 Jsoup 在网页上自动登录的帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21042446/