使用HtmlUnit时没有获得实际内容(响应是好的,即200但下载内容中缺少网页上显示的数据)

时间:2015-04-24 07:27:07

标签: java webclient htmlunit htmlunit-driver

我正在获取正确的htmlContent,同时将此代码用于其他网站,但一个网站无效(见下文)。请告知我还能做些什么?

public class Test3 {
        public static void main(String[] args) throws FailingHttpStatusCodeException, MalformedURLException, IOException {
            String url = "https : / / altera . wd1 . myworkdayjobs . com / Altera / jobs";
            String content = HtmlPageFetcher.fetchPage(url, "firefox", 50000);
            System.out.println(content);
    }

    public static String fetchPage(String url, String browser, long delayinmillis) {
        WebClient webClient;
        webClient = new WebClient(BrowserVersion.FIREFOX_17);
        webClient.getOptions().setCssEnabled(false);
        webClient.getOptions().setThrowExceptionOnScriptError(false);
        webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
        webClient.getOptions().setUseInsecureSSL(true);
        webClient.setAjaxController(new NicelyResynchronizingAjaxController());

        String content="";
        try {               
            HtmlPage page = webClient.getPage(url);
            webClient.waitForBackgroundJavaScript(delayinmillis);
            content = page.asXml();
            webClient.closeAllWindows();
            webClient = null;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if(webClient!=null)
                webClient.closeAllWindows();
        }
        return content;
    }

我也试过了CHROME和IE。网页上填写的数据是JSON(我通过chrome中的inspect元素看到的)

1 个答案:

答案 0 :(得分:0)

做了一个小修复。

请从maven或build server获取最新快照。