如何限制HtmlUnit的历史记录大小?

发布于 2024-09-02 00:47:33 字数 109 浏览 6 评论 0原文

我正在使用 HtmlUnit 进行解析工作,我发现 WebClient 保存每个 WebWindow 的历史记录会浪费内存。我根本不使用历史记录,我想禁用其管理或至少将其大小限制为 1 或 2。这可能吗?

I'm using HtmlUnit for a parsing job and I've discovered that the memory gets wasted with the WebClient holding the history for each WebWindow. I don't use the history at all and I'd like to disable its management or at least limit its size with 1 or 2. Is that possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

初心 2024-09-09 00:47:33

以下代码将 ignoreNewPages_ 设置为 true:

try {
    final WebClient webClient = getWebClient();
    final List<WebWindow> webWindows = webClient.getWebWindows();
    History window = webWindows.get(0).getHistory();
    Field f = window.getClass().getDeclaredField("ignoreNewPages_"); //NoSuchFieldException
    f.setAccessible(true);
    ((ThreadLocal<Boolean>) f.get(window)).set(true);
} catch (Exception e) {
    e.printStackTrace();
    throw new AssertionError("Can't disable history");
}

访问器:

private static WebTester getTester() {
    return JWebUnit.getTester();
}

private HtmlUnitTestingEngineImpl getHtmlUnitEngine() {
    return (HtmlUnitTestingEngineImpl) getTester().getTestingEngine();
}

private WebClient getWebClient() {
    return getHtmlUnitEngine().getWebClient();
}

The following code will set ignoreNewPages_ to true:

try {
    final WebClient webClient = getWebClient();
    final List<WebWindow> webWindows = webClient.getWebWindows();
    History window = webWindows.get(0).getHistory();
    Field f = window.getClass().getDeclaredField("ignoreNewPages_"); //NoSuchFieldException
    f.setAccessible(true);
    ((ThreadLocal<Boolean>) f.get(window)).set(true);
} catch (Exception e) {
    e.printStackTrace();
    throw new AssertionError("Can't disable history");
}

The accessors:

private static WebTester getTester() {
    return JWebUnit.getTester();
}

private HtmlUnitTestingEngineImpl getHtmlUnitEngine() {
    return (HtmlUnitTestingEngineImpl) getTester().getTestingEngine();
}

private WebClient getWebClient() {
    return getHtmlUnitEngine().getWebClient();
}
叫嚣ゝ 2024-09-09 00:47:33

据我所知,HtmlUnit 中没有选项可以禁用历史记录。 History 类有一个 getHistory() 方法,但没有 setHistory() 或disableHistory()。我所做的(当然并不理想)是释放网页并重新实例化它。只要您不释放 CookieManager,您在 cookie 方面就应该没问题。基本上,一旦我完成所有操作并登录,我会将当前页面存储到临时字符串后取消窗口,然后将其重新实例化到我离开的位置。我在特定点这样做是为了清除历史记录。

String tempPage = currentHtmlPage.getUrl().toString(); //HtmlPage class

window = null;

window = new WebWindow();

currentHtmlPage = new WebWindow.getWebClient().getPage(tempPage); //HtmlPage class

这允许窗口从上次停止的地方继续。
它很难看,但如果你绝望的话,它可能会起作用。

There is no option in HtmlUnit to disable history that I am aware of. The History class has a getHistory() method but no setHistory() or disableHistory(). What I have done, and it is certainly not ideal, is release the web page and reinstantiate it. As long as you don't release your CookieManager you should be ok on the cookie front. Basically once I get all the way through and logged in I nullify my window after storing the current page to a temp string, then I reinstantiate it to where I left off. I do this at given points to clear out history.

String tempPage = currentHtmlPage.getUrl().toString(); //HtmlPage class

window = null;

window = new WebWindow();

currentHtmlPage = new WebWindow.getWebClient().getPage(tempPage); //HtmlPage class

This allows the window to pick up where it left off.
Its ugly but if you are desperate, it may work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文