2013-03-16 71 views
2

我正在尝试使用HTMLUnit来生成我们的ajax页面的可抓取HTML快照(如https://developers.google.com/webmasters/ajax-crawling/所示)。这个想法是创建功能,允许企业通过定期的定期服务或根据自己的意愿创建快照。通过Tomcat运行HtmlUnit 7

我写了一个快速的POC主类来测试理论,并按预期工作(当我们查看源代码时,我们可以看到之前我们看不到的所有Google搜索器所需的数据)。我现在这个集成到我们的应用程序在Tomcat 7中运行,我在下载从谷歌的jquery.js与以下日志消息

2013-03-15 18:10:38,071 ERROR [author->taskExecutor-1] com.gargoylesoftware.htmlunit.html.HtmlPage  : Error loading JavaScript from [https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.js]. 
javax.net.ssl.SSLException: hostname in certificate didn't match: <ajax.googleapis.com/173.194.67.95> != <*.googleapis.com> OR <*.googleapis.com> OR <googleapis.com> 
at org.apache.http.conn.ssl.AbstractVerifier.verify(AbstractVerifier.java:228) 
at org.apache.http.conn.ssl.BrowserCompatHostnameVerifier.verify(BrowserCompatHostnameVerifier.java:54) 
at org.apache.http.conn.ssl.AbstractVerifier.verify(AbstractVerifier.java:149) 
at org.apache.http.conn.ssl.AbstractVerifier.verify(AbstractVerifier.java:130) 
at org.apache.http.conn.ssl.SSLSocketFactory.connectSocket(SSLSocketFactory.java:397) 
at org.apache.http.conn.ssl.SSLSocketFactory.connectSocket(SSLSocketFactory.java:495) 
at org.apache.http.conn.scheme.SchemeSocketFactoryAdaptor.connectSocket(SchemeSocketFactoryAdaptor.java:62) 
at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:148) 
at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:150) 

... 
没有被执行的AJAX

因此和快照的问题不包含我们希望它的视图源中的数据。有没有人知道为什么会出现在我的Tomcat版本的代码中,而不是在我的独立主类中?这两个版本都在我的本地机器上运行,其中一个仅在Tomcat(v7)中运行,另一个在Java应用程序中运行。两个版本都有相同的Maven包含(见底部)。

注意:我试过在指定一个BrowserVersion时指定WebClient client = new WebClient(BrowserVersion.FIREFOX_17);,因为我读过这样会产生更好的结果(对不起,我记不起链接)。再次,这在POC中工作正常,但是当我在Tomcat中运行时,我看到日志“Instatiating Web Client”,但无论等待多久,它永远不会到达“Client Instatiated”或抛出任何异常。我不知道这是否与无法下载jqeury.js有关,因为它仍然在没有指定BrowserVersion的POC中工作。

这里是工作

 OutputStreamWriter writer = null; 

     try { 
      final WebClient webClient = new WebClient(); 
      webClient.getOptions().setThrowExceptionOnScriptError(false); 
      webClient.getOptions().setPrintContentOnFailingStatusCode(false); 
      final HtmlPage page = (HtmlPage)webClient.getPage("http://myurl.com"); 

      webClient.waitForBackgroundJavaScript(1500); 

      File file = new File("C:\\test.html"); 
      FileUtils.touch(file); 

      writer = new OutputStreamWriter(new FileOutputStream(file), "UTF-8"); 
      writer.write(page.asXml()); 
      writer.flush(); 

     } catch (MalformedURLException mue) { 
      System.out.println("MalformedURL exception"); 
     } catch (IOException ioe) { 
      System.out.println("IOException occurred " + ioe.getMessage()); 
     } finally { 
      IOUtils.closeQuietly(writer); 
     } 

这里是我的集成版本

 /* Entry point for the generation */ 
    public void generate() { 

     log.info("Beginning snapshot generation..."); 

     try { 

      // Get the URLS 
      log.info("Retrieving list of page urls"); 
      List<String> pageUrls = getUrlList(); 
      log.info("Found {} urls to generate", pageUrls.size()); 

      // For every url we have generate a snapshot 
      for (String pageUrl: pageUrls) { 
       takeSnapshot(pageUrl); 
      } 
      log.info("Finished generating snapshots!"); 
     } catch (Exception e) { 
      log.error("Exception caught while generating snapshot", e); 
     } 
    } 

    /** 
    * Take the HTML snapshot of the url and output to the snapshot directory 
    */ 
    private void takeSnapshot(String pagePath) { 
     try { 
      String fullOutputFilePath = config.getHtmlSnapshotDirectory() + File.separator 
                 + pagePath + File.separator + HTML_SNAPSHOT_FILE_NAME; 
      String pageUrl = "http://myurl.com" + pagePath; 

      log.debug("Instantiating Web Client..."); 
      final WebClient webClient = new WebClient(); 
      log.debug("Client instantiated"); 
      webClient.getOptions().setThrowExceptionOnScriptError(false); 
      webClient.getOptions().setPrintContentOnFailingStatusCode(false); 
      final HtmlPage page = (HtmlPage)webClient.getPage(pageUrl); 

      webClient.waitForBackgroundJavaScript(1500); 

      snapshotFile = new File(fullOutputFilePath); 
      FileUtils.touch(snapshotFile); 

      writer = new OutputStreamWriter(new FileOutputStream(snapshotFile), "UTF-8"); 
      writer.write(page.asXml()); 
      writer.flush(); 
     } catch (MalformedURLException mue) { 
      System.out.println("MalformedURL exception"); 
     } catch (IOException ioe) { 
      System.out.println("IOException occurred " + ioe.getMessage()); 
     } finally { 
      IOUtils.closeQuietly(writer); 
     } 
    } 

Maven依赖我的POC Java的主要方法

 <dependency> 
      <groupId>net.sourceforge.htmlunit</groupId> 
      <artifactId>htmlunit</artifactId> 
      <version>2.12</version> 
     </dependency> 

     <dependency> 
      <groupId>org.apache.httpcomponents</groupId> 
      <artifactId>httpclient</artifactId> 
      <version>4.2.3</version> 
     </dependency> 

     <dependency> 
      <groupId>org.apache.httpcomponents</groupId> 
      <artifactId>httpcore</artifactId> 
      <version>4.3-alpha1</version> 
     </dependency> 

谢谢大家!

+0

我发现我可以使用'webClient.getOptions()。setUseInsecureSSL(true);'试图解决SSL问题。但是,当我将这行包含在其他'.getOptions()。set ...'statemtents之上时,代码只是挂在这一行上(就像它在指定浏览器版本时一样)。这意味着我仍然陷入困境。任何帮助非常感谢。 – DecafCoder 2013-03-18 10:21:01

回答

1

因此,加入webClient.getOptions().setUseInsecureSSL(true);是解决这个问题的关键。但是,我必须使用已弃用的版本webClient.setUseInsecureSSL(true);

我不知道为什么新版本在Tomcat中运行时不起作用,但它解决了问题。如果任何人都可以提供洞察,为什么这将是伟大的。我仍然失去了为什么在运行Tomcat时设置BrowserVersion导致应用程序暂停的原因。我已经向HtmlUnit邮件列表询问了这些问题的答案。