2016-03-31 60 views
0

我正在嘗試爲我的學校網站(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; 
} 

人都可以提供任何幫助,或任何人可以看看有什麼不對的代碼?

+0

也許你需要將JavaScriptEnabled設置爲true?只是想知道,因爲大多數現代網站需要啓用javascript。 –

+0

好吧,我試過了,很不幸,那並沒有工作,它仍然不認爲我登錄 – Andrew

回答

0

考慮使用Selenium庫與網站進行交互。它有編輯瀏覽器cookies的方法。 Here is a link to its java documentation.

+0

看起來很有希望,我會試一試!謝謝 – Andrew

+0

沒問題! @Andrew – Bautista

+0

不幸的是,這不是我正在尋找的,因爲它需要一個外部瀏覽器。我正在尋找的東西,將在Android工作室作爲webscraper – Andrew