java – 使用html单元提交表单

我想登录https://pacer.login.uscourts.gov/csologin/login.jsf.我使用html单位提交登录表单.这是我的代码:

public void submittingForm() throws Exception {
    final WebClient webClient = new WebClient();

    // Get the first page
    final HtmlPage page1 = webClient.getPage("https://pacer.login.uscourts.gov/csologin/login.jsf");

    // Get the form that we are dealing with and within that form, 
    // find the submit button and the field that we want to change.
    final HtmlForm form = page1.getFormByName("login");

   // final HtmlSubmitButton button = form.getButtonByName("login:j_idt184");
    final HtmlTextInput textField = form.getInputByName("login:loginName");
    final HtmlTextInput textField1 = form.getInputByName("login:password");
    // Change the value of the text field
    textField.setValueAttribute("XXX");
textField1.setValueAttribute("XXX");
    // Now submit the form by clicking the button and get back the second page.
    final HtmlPage page2 = form.getButtonByName("login:j_idt184").click();
System.out.println(page2.asText());
    webClient.closeAllWindows();
}

但它总是显示同样的错误:

Exception in thread "main" java.lang.IllegalAccessError: class com.gargoylesoftware.htmlunit.javascript.regexp.HtmlUnitRegExpProxy$FixedSubString cannot access its superclass net.sourceforge.htmlunit.corejs.javascript.regexp.SubString
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:791)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
    at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
    at com.gargoylesoftware.htmlunit.javascript.HtmlUnitContextFactory.makeContext(HtmlUnitContextFactory.java:227)
    at net.sourceforge.htmlunit.corejs.javascript.Context.enter(Context.java:439)
    at net.sourceforge.htmlunit.corejs.javascript.Context.call(Context.java:535)
    at net.sourceforge.htmlunit.corejs.javascript.ContextFactory.call(ContextFactory.java:538)
    at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.initialize(JavaScriptEngine.java:157)
    at com.gargoylesoftware.htmlunit.WebClient.initialize(WebClient.java:1141)
    at com.gargoylesoftware.htmlunit.WebWindowImpl.setEnclosedPage(WebWindowImpl.java:109)
    at com.gargoylesoftware.htmlunit.html.HTMLParser.parse(HTMLParser.java:200)
    at com.gargoylesoftware.htmlunit.html.HTMLParser.parseHtml(HTMLParser.java:179)
    at com.gargoylesoftware.htmlunit.DefaultPageCreator.createHtmlPage(DefaultPageCreator.java:221)
    at com.gargoylesoftware.htmlunit.DefaultPageCreator.createPage(DefaultPageCreator.java:106)
    at com.gargoylesoftware.htmlunit.WebClient.loadWebResponseInto(WebClient.java:433)
    at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:311)
    at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:373)
    at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:358)
    at htmlunit.HtmlUnit.submittingForm(HtmlUnit.java:63)
    at htmlunit.HtmlUnit.main(HtmlUnit.java:28)
Java Result: 1

如何克服上述错误并成功登录以上url.i我在我的代码中使用以下jar:
htmlunit2.9jar,公地code.jar等.哪个jar需要避免上面的错误.

请提供你的建议.

谢谢

阿图尔

解决方法:

我在我的系统上安装的HtmlUnit包括htmlunit-2.15.jar(让我们称之为“主”JAR)和htmlunit-core-js-2.15.jar(让我们称之为“corejs”JAR).查看两个JAR,我可以看到主JAR有com.gargoylesoftware.htmlunit.javascript.regexp.*,而corejs JAR有net.sourceforge.htmlunit.corejs.javascript.regexp.*:

> for i in htmlunit-2.15.jar htmlunit-core-js-2.15.jar; do echo "===== $i ====="; unzip -l $i| grep regexp; done;
===== htmlunit-2.15.jar =====
        0  06-02-2014 19:53   com/gargoylesoftware/htmlunit/javascript/regexp/
      768  06-02-2014 19:53   com/gargoylesoftware/htmlunit/javascript/regexp/HtmlUnitRegExpProxy$FixedSubString.class
     1846  06-02-2014 19:53   com/gargoylesoftware/htmlunit/javascript/regexp/HtmlUnitRegExpProxy$RegExpData.class
    11530  06-02-2014 19:53   com/gargoylesoftware/htmlunit/javascript/regexp/HtmlUnitRegExpProxy.class
     1549  06-02-2014 19:53   com/gargoylesoftware/htmlunit/javascript/regexp/RegExpJsToJavaConverter$Tape.class
     3777  06-02-2014 19:53   com/gargoylesoftware/htmlunit/javascript/regexp/RegExpJsToJavaConverter.class
===== htmlunit-core-js-2.15.jar =====
        0  05-30-2014 10:28   net/sourceforge/htmlunit/corejs/javascript/regexp/
      944  05-30-2014 10:28   net/sourceforge/htmlunit/corejs/javascript/regexp/CompilerState.class
      712  05-30-2014 10:28   net/sourceforge/htmlunit/corejs/javascript/regexp/GlobData.class
    42350  05-30-2014 10:28   net/sourceforge/htmlunit/corejs/javascript/regexp/NativeRegExp.class
     6850  05-30-2014 10:28   net/sourceforge/htmlunit/corejs/javascript/regexp/NativeRegExpCtor.class
     1077  05-30-2014 10:28   net/sourceforge/htmlunit/corejs/javascript/regexp/REBackTrackData.class
      700  05-30-2014 10:28   net/sourceforge/htmlunit/corejs/javascript/regexp/RECharSet.class
      819  05-30-2014 10:28   net/sourceforge/htmlunit/corejs/javascript/regexp/RECompiled.class
     1245  05-30-2014 10:28   net/sourceforge/htmlunit/corejs/javascript/regexp/REGlobalData.class
      687  05-30-2014 10:28   net/sourceforge/htmlunit/corejs/javascript/regexp/RENode.class
      918  05-30-2014 10:28   net/sourceforge/htmlunit/corejs/javascript/regexp/REProgState.class
    14443  05-30-2014 10:28   net/sourceforge/htmlunit/corejs/javascript/regexp/RegExpImpl.class
     1072  05-30-2014 10:28   net/sourceforge/htmlunit/corejs/javascript/regexp/SubString.class

所以我怀疑你成功地将主JAR包含在$CLASSPATH中,但不包括corejs JAR.因此,要解决此问题,您必须找到corejs JAR并将其包含在$CLASSPATH中.

通常,一旦安装,这两个JAR将存在于同一目录中.因此,如果您遇到这种情况,则可能更容易在$CLASSPATH中使用wildcard语法一次性获取它们.

上一篇:android – HttpURLConnection.getInputStream很慢


下一篇:手撸RPC