我正在尝试获取与HTMLunit的连接列表。到目前为止,我已经使用调试器(DebuggingWebConnection)。但是我只希望函数将连接作为列表返回。调试器也会下载所有内容。
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
任何人都有关于如何在没有调试器的情况下获取此列表的任何指示?
答案 0 :(得分:2)
我认为你不会找到一个简单的方法来做到这一点。主要是因为HtmlUnit是一个无头浏览器而不是在连接上运行的低级库(如HtmlUnit使用的Apache HttpClient类)。
您可能应该重新考虑使用HtmlUnit。如果您仍想使用HtmlUnit,那么我猜您必须查看DebuggingWebConection
的代码才能将其用作指导并创建您自己的WebConnection
实现,这可能会继承自WebConnectionWrapper
}。然后你必须自己处理连接列表的创建和维护。