好吧,我还没有找到一种方法,但我已经找到了一个有效的解决方法。尝试实现 。请看下面的示例代码。FalsifyingWebConnection
public class PinConnectionWrapper extends FalsifyingWebConnection {
public PinConnectionWrapper(WebClient webClient)
throws IllegalArgumentException {
super(webClient);
}
@Override
public WebResponse getResponse(WebRequest request) throws IOException {
WebResponse res = super.getResponse(request);
if(res.getWebRequest().getUrl().toString().endsWith("/toolbar.js")) {
return createWebResponse(res.getWebRequest(), "",
"application/javascript", 200, "Ok");
}
return res;
}
}
在上面的代码中,每当HtmlUnit请求工具栏时.js我的代码只会返回一个虚假的空响应。您可以将上面的包装器类插入到 HtmlUnit 中,如下所示。
final WebClient webClient = new WebClient(BrowserVersion.FIREFOX_3_6);
new PinConnectionWrapper(webClient);