【问题标题】:HtmlUnit parses "http://www.ean-search.org/sitemap.html" with 404 returnHtmlUnit 解析“http://www.ean-search.org/sitemap.html”并返回 404
【发布时间】:2013-08-07 18:22:55
【问题描述】:

我正在尝试获取并解析页面“http://www.ean-search.org/sitemap.html”,但它总是出现 404 错误和空白页面。所有文本内容区域都是空白的。

我尝试了 HtmlUnit webclient 的许多选项配置,例如.setThrowExceptionOnFailingStatusCode(false)、setThrowExceptionOnScriptError(true)、setRedirectEnabled(false)、setJavaScriptEnabled(true)、setThrowExceptionOnScriptError(false)。 他们都没有工作......

有人有什么建议吗?谢谢。

ps:我的网络客户端代码:

myWebClient = new WebClient(BrowserVersion.FIREFOX_3_6); 
myWebClient.setIncorrectnessListener(new CustomizedInconnectnessListener());
myWebClient.setTimeout(180000); //3 min, used twice, first for connection, second for retrieval
try {
    myWebClient.setUseInsecureSSL(true);
} catch (GeneralSecurityException ex) {
    logger.log(Level.SEVERE, "cannot set UseInsecureSSL for BNP webclient",ex);
            //ignore it, continue
}

myWebClient.setRedirectEnabled(true); 
myWebClient.setCssEnabled(false); 
myWebClient.setJavaScriptTimeout(30000); //timeout for executing java script
myWebClient.setThrowExceptionOnScriptError(false);

HtmlPage htmlpage = (HtmlPage) myWebClient.getHtmlPage("http://www.ean-search.org/sitemap.html");
myWebClient.waitForBackgroundJavaScriptStartingBefore(3000);
Thread.sleep(3000);
System.out.println(htmlpage.asXml());

【问题讨论】:

    标签: http-status-code-404 htmlunit


    【解决方案1】:

    嗯,您使用的代码看起来很糟糕。我收到了很多错误和警告......甚至无法编译它。例如myWebClient.getHtmlPage 应该是myWebClient.getPage

    此代码适用于我并输出页面内容:

    WebClient myWebClient = new WebClient(BrowserVersion.FIREFOX_17);
    HtmlPage page = myWebClient.getPage("http://www.ean-search.org/sitemap.html");
    System.out.println(page.asXml());
    

    确保您使用最新的 HtmlUnit 库,并且当编译器告诉您某些内容已被弃用时,请注意编译器。不建议使用这些方法和字段。

    【讨论】:

    • 好吧,我忘了说那些只是代码 sn-p,方法 '.getHtmlPage' 是我自己内部实现的。无论如何,感谢您指出这一点。
    • 至于使用最新的 HtmlUnit 库,有时由于组件之间的相互依赖关系,这非常困难。如果您在一些大型项目中拥有大量lib、接口和依赖项,您应该了解它。当然,我会尽量使用最新版本的 HtmlUnit。感谢您的建议。
    猜你喜欢
    • 2016-10-19
    • 1970-01-01
    • 2013-11-10
    • 2022-11-11
    • 2011-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多