Search code examples
javahtmlunithtmlunit-driver

HtmlUnit + Webclient.getPage throws


I am trying to access a URL (admin.do) directly after logging into the application and the URL is a restricted page. However, when I run the scenario on browser, I am able to see the page getting redirected to access denied page. However, when I run the same with htmlunit, I see the following exception instead.

com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException: 403 Forbidden for https://localhost:27216/app/admin.do
    at com.gargoylesoftware.htmlunit.WebClient.throwFailingHttpStatusCodeExceptionIfNecessary(WebClient.java:536)
    at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:331)
    at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:387)
    at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:372)

Any idea how to achieve this? Here is my code.

HtmlPage resp = null;
WebClient webClient = getWebClient();
HtmlPage page = (HtmlPage) webClient.getPage(url);

Solution

  • Got it working. The exception is thrown and hence it doesn't allow the page to follow the redirect. I set the property throwExceptionOnFailingStatusCode on the WebClient to false, so that the the exception is ignored.

    HtmlPage resp = null;
    WebClient webClient = getWebClient();
    webClient.setThrowExceptionOnFailingStatusCode(false);
    HtmlPage page = (HtmlPage) webClient.getPage(url);