我正在嘗試爲我的學校網站(reggienet.illinoisstate.edu)提出申請,但我無法進入登錄並將其存儲在我所在的Cookie中登錄了。我搜索了網頁並嘗試了所有可以找到的東西,但沒有任何工作,我完全迷失了。使用htmlUnit登錄到學校網站
這裏是我的代碼,現在
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;
}
人都可以提供任何幫助,或任何人可以看看有什麼不對的代碼?
也許你需要將JavaScriptEnabled設置爲true?只是想知道,因爲大多數現代網站需要啓用javascript。 –
好吧,我試過了,很不幸,那並沒有工作,它仍然不認爲我登錄 – Andrew