我正在尝试为我的学校网站(reggienet.illinoisstate.edu)申请,但我在输入登录信息并将其存储在我登录的cookies中时遇到了问题。我已经在网上找遍了,也试过所有我能找到的东西,但是什么都没有用,我完全迷路了。
这是我现在拥有的代码
public void login()
{
HtmlPage currentPage = null;
Scanner keyboard = new Scanner(System.in);
WebClient webClient = new WebClient();
try
{
webClient = reggieLogin(webClient, keyboard);
}
catch (Exception e)
{
e.printStackTrace();
}
try
{
currentPage = webClient.getPage(""https://reggienet.illinoisstate.edu/portal"");
}
catch (FailingHttpStatusCodeException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (MalformedURLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
String pageSource = currentPage.asXml();
System.out.print(pageSource);
}
private WebClient reggieLogin(WebClient webClient, Scanner keyboard) throws Exception
{
webClient.getOptions().setJavaScriptEnabled(false);
HtmlPage currentPage = webClient.getPage("https://account.illinoisstate.edu/centrallogin/"); //Load page at the STRING address.
HtmlInput username = currentPage.getElementByName("username"); //Find element called loginuser for username
System.out.print("ULID: ");
username.setValueAttribute(keyboard.nextLine()); //Set value for username
HtmlInput password = currentPage.getElementByName("password"); //Find element called loginpassword for password
System.out.print("Password: ");
password.setValueAttribute(keyboard.nextLine()); //Set value for password
HtmlButtonInput submitBtn = currentPage.getElementByName("submit"); //Find element called Submit to submit form.
currentPage = submitBtn.click(); //Click on the button.
return webClient;
}有人能提供任何帮助吗,或者有人能看到这段代码的问题所在吗?
发布于 2016-03-31 11:01:57
考虑使用Selenium库与网站进行交互。它具有编辑浏览器cookie的方法。Here is a link to its java documentation.
https://stackoverflow.com/questions/36323023
复制相似问题