2017-08-26 20 views
-2

我想用HTMLUnit来访问用户名和密码的网站的html。 https://186.springfield.k12.il.us/IS3/是我的学校发布成绩的地方。我只知道Java,在这方面我不太擅长,这是我的第一个问题。我会很感激有人帮助我完成这项工作。下面是我有:试图用java访问安全的网站

public static void main(String[] args) { 
    WebClient webClient = new WebClient(); 
    try { 
     HtmlPage page = (HtmlPage) webClient 
       .getPage("https://186.springfield.k12.il.us/IS3/"); 
     HtmlForm form = page.getFormByName("login_form"); 
     form.getInputByName("loginuseridvar").setValueAttribute("myusername"); 
     HtmlInput passWordInput = form.getInputByName("password"); 
     passWordInput.removeAttribute("disabled"); 
     passWordInput.setValueAttribute("mypassword"); 

     page = form.getInputByValue("Log On").click(); 

     System.out.println(page.asText()); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } finally { 
     webClient.close(); 
    } 
} 
+1

你的问题是什么? –

+1

请澄清您的具体问题或添加其他详细信息,以突出显示您的需要。正如目前所写,很难确切地说出你在问什么。请参阅[如何提问](https://stackoverflow.com/help/how-to-ask)页面以获得帮助,以澄清此问题 –

+0

我正在努力使其工作,我无法弄清楚为什么它没有。 –

回答

0

试试这个

try (WebClient webClient = new WebClient()) { 
     // page has an js error (same as with real browsers) 
     // ignore these errors 
     webClient.getOptions().setThrowExceptionOnScriptError(false); 

     HtmlPage page = (HtmlPage) webClient.getPage("https://186.springfield.k12.il.us/IS3/"); 
     // System.out.println(page.asXml()); 

     // form has no name, we use the first one 
     HtmlForm form = page.getForms().get(0); 
     form.getInputByName("loginuseridvar").type("myusername"); 

     // there seem to be some (async) magic that clears the password field 
     // force this magic and wait until done otherwise the js will clear the password 
     // after we typed it in and before the click will be processed 
     form.getInputByName("password").focus(); 
     webClient.waitForBackgroundJavaScript(100); 
     form.getInputByName("password").type("mypassword"); 

     page = form.getInputByName("login").click(); 
     webClient.waitForBackgroundJavaScript(1000); 

     System.out.println(page.asXml()); 
    } 

希望这些意见将帮助你有点明白了是怎么回事。因为我没有真实账户,所以我只能测试错误情况。 如果你仍然有问题,你必须通过私人邮件向我发送一些凭证。

BTW:页面本身有一些缺陷(破解的javascript,XHTML语法错误)