簡體   English   中英

HtmlUnit HtmlSubmitInput.click()導致“錯誤的URL”更正為“cgi-bin”,然后導致UnknownHostException

[英]HtmlUnit HtmlSubmitInput.click() results in “Incorrect URL” corrected to “cgi-bin” which then leads to an UnknownHostException

我正在嘗試編寫一個應該訪問此站點的小機器人http://lsa.colorado.edu/cgi-bin/LSA-pairwise.html ,在textarea中輸入一些文本並通過按下提交來獲取生成的頁面提交按鈕。 這是一個語言學項目。 但是,當我執行單擊HtmlSubmitInput按鈕時,返回的URL似乎格式不正確,因為IncorrectnessListenerImpl通知我:

Apr 10, 2016 2:38:35 PM com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl notify
WARNUNG: Incorrect URL "http:/cgi-bin/LSA-pairwise-x.html" has been corrected

網址應該是

http://lsa.colorado.edu/cgi-bin/LSA-pairwise-x.html

然后導致以下堆棧跟蹤(由於長度縮短):

Exception in thread "main" java.lang.RuntimeException: java.net.UnknownHostException: cgi-bin: unknown error
    at com.gargoylesoftware.htmlunit.WebClient.download(WebClient.java:2078)
    at com.gargoylesoftware.htmlunit.html.HtmlForm.submit(HtmlForm.java:141)
    at com.gargoylesoftware.htmlunit.html.HtmlSubmitInput.doClickStateUpdate(HtmlSubmitInput.java:90)
    at com.gargoylesoftware.htmlunit.html.DomElement.click(DomElement.java:795)
    at com.gargoylesoftware.htmlunit.html.DomElement.click(DomElement.java:742)
    at com.gargoylesoftware.htmlunit.html.DomElement.click(DomElement.java:689)
    at LSABot.submitInput(LSABot.java:30)
    at Start.main(Start.java:8)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
 [...]

我的猜測是HtmlUnit嘗試修復URL但這只會導致“cgi-bin”,這當然是格式錯誤的。 我一遍又一遍地搜索,但沒有發現任何與我的問題相關的內容。

我的LSABot類:

public class LSABot {
    final WebClient webClient;
    private HtmlPage mainPg, rsltPg;
    private HtmlForm htmlForm;
    private HtmlTextArea txtA;
    private HtmlSubmitInput submitBt;

    public LSABot () throws Exception {
        this.webClient = new WebClient(BrowserVersion.CHROME);
        this.webClient.getOptions().setJavaScriptEnabled(true);
        this.mainPg = this.webClient.getPage("http://lsa.colorado.edu/cgi-bin/LSA-pairwise.html");
        this.htmlForm = this.mainPg.getForms().get(0);
        this.txtA = this.htmlForm.getTextAreaByName("txt1");
        this.submitBt = this.htmlForm.getInputByValue("Submit Texts");
    }

    public void submitInput(String input) {
        this.txtA.setText(input);
        try {
            this.rsltPg = this.submitBt.click();
            this.webClient.waitForBackgroundJavaScript(30*1000);
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
    }

錯誤來自表單的html內容。 action屬性應該是http://lsa.colorado.edu/cgi-bin/LSA-pairwise-x.html而不是http:/cgi-bin/LSA-pairwise-x.html

試試這段代碼,它應該有效:

LogFactory.getFactory().setAttribute("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.NoOpLog");

java.util.logging.Logger.getLogger("com.gargoylesoftware").setLevel(Level.OFF); 
java.util.logging.Logger.getLogger("org.apache.commons.httpclient").setLevel(Level.OFF);

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

String url = "http://lsa.colorado.edu/cgi-bin/LSA-pairwise.html";
final HtmlPage page = client.getPage(url);

HtmlForm htmlForm = page.getForms().get(0);
HtmlTextArea txtA = htmlForm.getTextAreaByName("txt1");
txtA.setText("hello");
HtmlSubmitInput submitBt = htmlForm.getInputByValue("Submit Texts");

// change the form action attribute to the correct one  
htmlForm.setAttribute("action", "http://lsa.colorado.edu/cgi-bin/LSA-pairwise-x.html");

HtmlPage page2 = submitBt.click();
System.out.println(page2.asText());

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM