How to limit the size of an HtmlUnit story?

I use HtmlUnit for parsing and I find that memory is lost in a WebClient that stores a history for each WebWindow. I don’t use history at all, and I would like to disable its management or at least limit its size to 1 or 2. Is this possible?

+4
source share
2 answers

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"); } 

Accessories:

 private static WebTester getTester() { return JWebUnit.getTester(); } private HtmlUnitTestingEngineImpl getHtmlUnitEngine() { return (HtmlUnitTestingEngineImpl) getTester().getTestingEngine(); } private WebClient getWebClient() { return getHtmlUnitEngine().getWebClient(); } 
+2
source

In HtmlUnit there is no way to disable the story that I know of. The History class has a getHistory () method, but not setHistory () or disableHistory (). What I did, and this is certainly not perfect, is to free up the web page and restore it. Until you issue a CookieManager, you should be fine at the front of the cookie. Basically, as soon as I completely go through and log into the system, I reset my window after saving the current page in the temp line, and then again create it where I left off. I do this at certain points to clear the story.

 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 the place where it was stopped. Its ugly, but if you are desperate, this might work.

-1
source

Source: https://habr.com/ru/post/1309989/


All Articles