English 中文(简体)
使用 Html 日志登录
原标题:Login Using HtmlUnit

我对Html United非常新。 我想知道我是否有能力使用 htmlunit 登录网站, 并在网站进行一些操作, 例如, 我想登录我的办公室门户,

I am using html unit and it shows some errors, is its possible to do with html unit or are there any other tools I can use for this purpose...
Here is my code
final WebClient webClient = new WebClient(BrowserVersion.FIREFOX_3_6);        
webClient.setJavaScriptEnabled(true);
webClient.getCookieManager().setCookiesEnabled(true);

final HtmlPage page1 =  webClient.getPage("http://www.ccstechnologies.org/login.aspx/");
final HtmlForm form = page1.getFormByName("form1");         
final HtmlSubmitInput button =  form.getInputByName("BtnLogin");
final HtmlTextInput textField =  form.getInputByName("Username");
final HtmlPasswordInput pwd =  form.getInputByName("password");        
textField.setValueAttribute("username");
pwd.setValueAttribute("password");      

final HtmlPage page2 =  button.getEnclosingForm().click();  
String htmlBody = page2.getWebResponse().getContentAsString(); 

System.out.println("Base Uri 1 : "+page1);
System.out.println("Base Uri 2 : "+page2);

webClient.closeAllWindows();

But when I print page2 it shows only the login page s url, and it is not returning the homepage url. What could be the problem ?

这是我在控制台 点击表单时得到的

May 28, 2012 11:44:15 AM com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl notify WARNING: Obsolete content type encountered: application/x-javascript . May 28, 2012 11:44:16 AM com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl notify WARNING: Obsolete content type encountered: application/x-javascript . Base Uri 1 : HtmlPage(http://www.ccstechnologies.org/login.aspx/)@2741851 Base Uri 2 : HtmlPage(http://www.ccstechnologies.org/login.aspx/)@2741851

点击按钮时产生的结果

May 29, 2012 10:00:02 AM com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl notify
WARNING: Obsolete content type encountered:  application/x-javascript .
May 29, 2012 10:00:02 AM com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl notify
WARNING: Obsolete content type encountered:  application/x-javascript .
May 29, 2012 10:00:03 AM com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl notify
WARNING: Obsolete content type encountered:  application/x-javascript .
May 29, 2012 10:00:03 AM com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl notify
WARNING: Obsolete content type encountered:  application/x-javascript .
May 29, 2012 10:00:03 AM com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl notify
WARNING: Obsolete content type encountered:  application/x-javascript .
May 29, 2012 10:00:03 AM com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl notify
WARNING: Obsolete content type encountered:  application/x-javascript .
May 29, 2012 10:00:03 AM com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl notify
WARNING: Obsolete content type encountered:  text/javascript .
May 29, 2012 10:00:03 AM com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl notify
WARNING: Obsolete content type encountered:  text/javascript .
May 29, 2012 10:00:03 AM com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl notify
WARNING: Obsolete content type encountered:  text/javascript .
May 29, 2012 10:00:03 AM com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl notify
WARNING: Obsolete content type encountered:  text/javascript .
May 29, 2012 10:00:03 AM com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl notify
WARNING: Obsolete content type encountered:  text/javascript .
May 29, 2012 10:00:03 AM com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl notify
WARNING: Obsolete content type encountered:  text/javascript .
May 29, 2012 10:00:03 AM com.gargoylesoftware.htmlunit.DefaultCssErrorHandler error
WARNING: CSS error: [259:24] Error in expression. Invalid token "=". Was expecting one of: <S>, <COMMA>, "/", <PLUS>, "-", <HASH>, <STRING>, ")", <URI>, "inherit", <EMS>, <EXS>, <LENGTH_PX>, <LENGTH_CM>, <LENGTH_MM>, <LENGTH_IN>, <LENGTH_PT>, <LENGTH_PC>, <ANGLE_DEG>, <ANGLE_RAD>, <ANGLE_GRAD>, <TIME_MS>, <TIME_S>, <FREQ_HZ>, <FREQ_KHZ>, <DIMENSION>, <PERCENTAGE>, <NUMBER>, <FUNCTION>, <IDENT>.
May 29, 2012 10:00:03 AM com.gargoylesoftware.htmlunit.DefaultCssErrorHandler error
WARNING: CSS error: [259:29] Error in style rule. Invalid token "
   ". Was expecting one of: "}", ";".
May 29, 2012 10:00:03 AM com.gargoylesoftware.htmlunit.DefaultCssErrorHandler warning
WARNING: CSS warning: [259:29] Ignoring the following declarations in this rule.
HtmlPage(http://192.168.0.5/login.aspx)@23511316
HtmlPage(http://192.168.0.5/login.aspx)@17700115
问题回答

好吧,我调查了一下,似乎问题在于按钮。我用这个替换了您按钮的代码线:

 final HtmlPage page2 =  (HtmlPage) form.getInputByValue("Login").click();

now it appears that it at least tries to login(and the page of course prints invalid login) so it should work with appropriate credentials. to print the page in java and see it use system.out.println(page1.asText()) or asXml depending on what you want to see

我的代码最后是这样的:

final WebClient webClient = new WebClient(BrowserVersion.FIREFOX_3_6);         
        webClient.setJavaScriptEnabled(true);
        webClient.getCookieManager().setCookiesEnabled(true);


     try{   final HtmlPage page1 =  webClient.getPage("http://www.ccstechnologies.org/login.aspx/");
        final HtmlForm form = page1.getFormByName("form1");         
        final HtmlSubmitInput button =  form.getInputByName("BtnLogin");
        final HtmlTextInput textField =  form.getInputByName("Username");
        final HtmlPasswordInput pwd =  form.getInputByName("password");        
        textField.setValueAttribute("username");
        pwd.setValueAttribute("password");      
System.out.println(page1.asText());
        final HtmlPage page2 =  (HtmlPage) form.getInputByValue("Login").click();

        String htmlBody = page2.getWebResponse().getContentAsString(); 
        System.out.println(page2.asText());
       System.out.println("Base Uri 1 : "+page1);
      System.out.println("Base Uri 2 : "+page2);

        webClient.closeAllWindows();}catch (Exception e) {
            // TODO: handle 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);

这应该能像对我一样解决问题

try to set cookies enabled, and try to set javascript enabled ignore errors that it might print...(I used to think errors in red are bad, in html unit it seems not necessarily)

If the website uses an ajax call to login. This worked for me. Set this

webClient.setAjaxController(new NicelyResynchronizingAjaxController());

这将使所有的刺死电话同步

这就是我如何设置我的 WebClient 对象的方法

WebClient webClient = new WebClient(BrowserVersion.CHROME);
webClient.getOptions().setJavaScriptEnabled(true);
webClient.getOptions().setCssEnabled(false);
webClient.getOptions().setUseInsecureSSL(true);
webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
webClient.getCookieManager().setCookiesEnabled(true);
webClient.setAjaxController(new NicelyResynchronizingAjaxController());
webClient.getOptions().setThrowExceptionOnScriptError(false);
webClient.getCookieManager().setCookiesEnabled(true);




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签