2013-09-21 70 views
1

我正在嘗試獲取與HTML單元連接的列表。到目前爲止,我已經與調試器(DebuggingWebConnection)一起工作。不過,我只是希望該函數將連接作爲列表返回。調試器也下載一切。HTMLUnit - 如何獲取作爲列表的傳出連接列表?

public static void crawlPage() throws Exception { 
    final WebClient wc = new WebClient(); 
    String resultFolder = "results"; 
    final WebConnection connection = new DebuggingWebConnection(wc.getWebConnection(), resultFolder); 
    wc.setWebConnection(connection); 
    HtmlPage page = (HtmlPage) wc.getPage("http://www.test.com"); 

} 

返回results/index.html;

GET http://www.test.com/ 200 (text/html) 14.53 Kib, 866ms 
GET http://www.google-analytics.com/ga.js 200 (text/javascript) 77.38 Kib, 399ms 
GET http://www.google-analytics.com/__utm.gif?utmwv=5.4.5&utms=1&utmn=1203166335&utmhn=www.test.com&utmcs=utf-8&utmsr=1024x768&utmvp=1256x605&utmsc=24-bit&utmul=en-us&utmje=0&utmfl=-&utmdt=Create%20Tests%20for%20Organizational%20Training%20and%20Certification%20Programs%20%E2%80%93%20Test.com&utmhid=121499150&utmr=-&utmp=%2F&utmht=1379757739907&utmac=UA-3518049-1&utmcc=__utma%3D99889051.1275286197.1379757740.1379757740.1379757740.1%3B%2B__utmz%3D99889051.1379757740.1.1.utmcsr%3D(direct)%7Cutmccn%3D(direct)%7Cutmcmd%3D(none)%3B&utmu=q~ 200 (image/gif) 0.03 Kib, 23ms 
GET http://www.test.com/content/js/jquery.min.js 200 (application/x-javascript) 166.71 Kib, 981ms 
GET http://www.test.com/content/style.css 200 (text/css) 15.44 Kib, 323ms 
GET http://www.test.com/content/flexslider.css 200 (text/css) 2.68 Kib, 395ms 
GET http://www.test.com/content/jquery.fancybox-1.3.4.css 200 (text/css) 8.76 Kib, 313ms 
GET http://www.test.com/content/jwplayer/jwplayer.js 200 (application/x-javascript) 326.94 Kib, 724ms 
GET http://www.test.com/content/js/jquery.flexslider-min.js 200 (application/x-javascript) 21.44 Kib, 320ms 
GET http://www.test.com/content/js/jquery.fancybox-1.3.4.js 200 (application/x-javascript) 33.31 Kib, 364ms 
GET http://www.test.com/content/js/functions.js 200 (application/x-javascript) 1.87 Kib, 223ms 
GET http://www.test.com/assets/_video_thumbnail/47-9a8803a2.png 200 (image/png) 223.8 Kib, 692ms 
GET http://l.longtailvideo.com/download/5/9/logo.png 200 (image/png) 1.8 Kib, 26ms 
GET http://www.test.com/assets/_video_thumbnail/92-7b078568.png 200 (image/png) 213.78 Kib, 1421ms 
GET http://www.test.com/assets/_video_thumbnail/90-d8d9524d.png 200 (image/png) 229.09 Kib, 595ms 
GET http://www.test.com/assets/_video_thumbnail/93-52d92cf8.png 200 (image/png) 221.17 Kib, 587ms 

任何人有任何指針如何獲得這個列表沒有調試器?

回答

2

我不認爲你會找到一個簡單的方法來做到這一點。主要是因爲HtmlUnit是一個無頭瀏覽器,並不是像這樣的低級庫來操作連接(就像HtmlUnit使用的Apache HttpClient類)。

也許你應該重新考慮使用HtmlUnit。如果你仍然想使用HtmlUnit,那麼我想你必須看到DebuggingWebConection的代碼才能使用它作爲指導,並創建自己的WebConnection實現,這可能會從WebConnectionWrapper繼承。然後你必須自己處理連接列表的創建和維護。

相關問題