2015-09-28 57 views
0

用java eclipse登錄學校郵件我正在創建一個應用程序,可以從我的學校網站上獲取信息,就像我的課程安排和作業一樣,這樣我就不必登錄每次我想獲得這個信息。簡單。HTMLUnit用https://

這是我用來打電話給網站的主要課程。

public class HttpRequestsExample { 

final private static String email = "email"; 
final private static String password = "password"; 
static HtmlPage secondPage; 

public static WebClient tremorLogin(WebClient webClient) throws Exception { 

    webClient.getOptions().setJavaScriptEnabled(false); 
    webClient.getOptions().setRedirectEnabled(true); 
    webClient.getOptions().setThrowExceptionOnScriptError(false); 
    webClient.getOptions().setCssEnabled(false); 
    webClient.getOptions().setUseInsecureSSL(true); 
    webClient.getOptions().setThrowExceptionOnFailingStatusCode(false); 
    webClient.getCookieManager().setCookiesEnabled(true); 

    HtmlPage currentPage = webClient.getPage("school login site"); 

    final HtmlForm form = currentPage.getFormByName("loginf"); 

    final HtmlTextInput textFieldName = form.getInputByName("j_username"); 
    final HtmlPasswordInput textFieldPass = form.getInputByName("j_password"); 
    textFieldName.setValueAttribute(email); 
    textFieldPass.setValueAttribute(password); 
    final HtmlSubmitInput button = form.getInputByValue(" Login "); 
    secondPage = button.click(); 

    return webClient; 

} 

public static void main(String[] args) throws Exception { 


    WebClient webClient = new WebClient(BrowserVersion.CHROME); 
    webClient = tremorLogin(webClient); 

    String pageSource = secondPage.asXml(); 

    System.out.println(pageSource); 
} 
} 

這裏是我試圖去的形式,但它是說「loginf」不存在。

<form name="loginf" action="/idp/Authn/UserPassword" method="post" autocomplete="off"> 

       <td> 
       &nbsp;<input name="j_username" type="text" tabindex="1" style="width:150px" autocapitalize="off"> 
       </td> 
      </tr> 
      <tr> 
       <td align="right"> 
       <b>Password:</b></td> 
       <td> 
       &nbsp;<input name="j_password" type="password" tabindex="2" style="width:150px"> 
       </td> 
      </tr> 

      </tbody></table> 
      </form> 

以下是錯誤,我得到

Exception in thread "main" com.gargoylesoftware.htmlunit.ElementNotFoundException: elementName=[form] attributeName=[name] attributeValue=[loginf] 
at com.gargoylesoftware.htmlunit.html.HtmlPage.getFormByName(HtmlPage.java:638) 
at HttpRequestsExample.tremorLogin(HttpRequestsExample.java:34) 
at HttpRequestsExample.main(HttpRequestsExample.java:51) 
+0

請使用最新版本,或[建設](https://ci.canoo.com /teamcity/viewLog.html?buildTypeId=HtmlUnit_FastBuild&buildId=lastSuccessful&tab=artifacts),'setThrowExceptionOnScriptError(true)',並打印'currentPage.asXml()'以查看HtmlUnit所看到的內容。否則,如果可能的話,請張貼您的URL –

回答

0

使用這個代替:

HtmlPage currentPage = webClient.getPage("school login site"); 
HtmlTextInput textField2 = (HtmlTextInput) currentPage.getElementByName("j_username"); 
textField2.setValueAttribute(email); 
HtmlTextInput textField3 = (HtmlTextInput) currentPage.getElementByName("j_password"); 
textField3.setValueAttribute(password);