我已经阅读了许多类似的问题,但我仍然坚持登录我的学校成绩簿 (https://parents.mtsd.k12.nj.us/genesis/parents) 来检索数据。我的网络类(class)如下所示:
public class WebLogin {
public String login(String username, String password, String url) throws IOException {
URL address = new URL(url);
HttpURLConnection connection = (HttpURLConnection) address.openConnection();
connection.setDoOutput(true);
connection.setRequestProperty("j_username", username);
connection.setRequestProperty("j_password", password);
connection.setRequestProperty("__submit1__","Login");
InputStream response = connection.getInputStream();
Document document = Jsoup.parse(response, null, "");
//don't know what to do here!
return null;
}
}
我不确定如何处理 InputStream 或者我是否要正确登录,我再次浏览并阅读了一些在线资源以试图理解这个概念,但我仍然感到困惑。任何帮助表示赞赏!
更新:所以现在我了解了基本过程,我知道我必须做什么。使用 Jsoup 处理连接时,我知道您可以执行以下操作:
Connection.Response res = Jsoup.connect("---website---")
.data("j_username", username, "j_password", password)
.followRedirects(true)
.method(Method.POST)
.execute();
但是,对于如何实际发送数据(例如使用 HttpURLConnection 将用户的用户名/密码发送到网站,以及如何实际存储获取的 cookie...我对其他一切都很好
最佳答案
一种方法是使用 Apache Http Client .
也许像下面这样的东西可以工作。
Request.Post("https://parents.mtsd.k12.nj.us/genesis/j_security_check")
.bodyForm(
Form.form()
.add("j_username", username)
.add("j_password", password).build()
)
.execute().returnContent();
您将弄清楚登录所需的信息(URL、请求方法、名称和参数值等)。
这是我为 logging into Gmail 找到的示例.
关于java - 需要帮助登录网站和检索信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31629938/