2

In HtmlUnit, how to disable throw exception when the requested page returns fail status code (like 4xx)? I need to get the status code, so if it throws an exception, I can't get the status code.

Page page = null;
try {
    page = webClient.getPage(requestSettings);
    System.out.println(page.getWebResponse().getStatusCode()); // it doesn't go to this line because exception is already thrown
} catch (Exception e) {
    System.out.println(page.getWebResponse().getStatusCode()); // it will fail because of NullPointerException
    System.out.println(e);
}

The following method seems to work only on older versions of HtmlUnit. I'm using v2.25 and the method doesn't exist.

webClient.setThrowExceptionOnFailingStatusCode(false);

1 Answer 1

3

The new API now has WebClientOptions,

you should use:

webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.