2013-03-16 141 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郵件列表詢問了這些問題的答案。