如何登录httpunit(带表格)

时间:2016-01-31 06:07:20

标签: java htmlunit

如何使用httpunit进行网络登录? 我对HtmlUnit非常新。 我想登录但是 这段代码我找不到任何形式.. 我怎么办登录..? 请帮忙......

<body onload="$.app.fn_init();">
<div id="login">
  <p><img src="../images/logo.gif" /></p>
  <p class="topt"><span id="sp_campus"></span> REG</p>

  <div class="idpw"> <span class="idpwtxt">ID</span>
    <input tabindex="1" maxLength=20 name="USER_ID" type="text" class="input1" id="USER_ID" onkeydown="javascript:$.app.fn_enterUserId(event);">
    <span class="idpwtxt">PASSWORD</span>
    <input tabindex="2" name="PWD" maxLength=20 type="password" class="input1" id="PWD" onkeydown="javascript:$.app.fn_enterPwd(event);">
  </div>
  <input tabindex="3" name="button" type="button" class="loginbtn" id="button" value="LOGIN" onClick="javascript:$.app.fn_login();" onkeydown="javascript:$.app.fn_enterPwd(event);">
  <input tabindex="4" name="button" type="button" class="findbtn" id="button" value="FIND" onClick="javascript:$.app.fn_openPopup();" onkeydown="javascript:$.app.fn_openPopup(event);">

  <div class="notice">
    <p class="noticetitle"></p>
    <div class="viewer">

    </div>
  </div>
  <div class="footer">Copyright<br>
    All Rights Reserved.</div>

1 个答案:

答案 0 :(得分:0)

试试这个:

    WebClient client = new WebClient(BrowserVersion.CHROME);
    client.getOptions().setJavaScriptEnabled(true);
    client.getOptions().setThrowExceptionOnScriptError(false);
    client.getOptions().setThrowExceptionOnFailingStatusCode(false);

    HtmlPage page = client.getPage("http://yoursite.com/");
    HtmlTextInput user = page.getElementByName("USER_ID");
    user.setValueAttribute("youruser");
    HtmlPasswordInput password = page.getElementByName("PWD");
    password.setValueAttribute("yourpassword");

    HtmlButtonInput loginButton = (HtmlButtonInput) page.getByXPath("//input[@class='loginbtn']").get(0);
    page = loginButton.click();

    System.out.println(page.asText());

    client.close();