HtmlUnit + Webclient.getPage 抛出

HtmlUnit + Webclient.getPage throws

我试图在登录应用程序后直接访问 URL (admin.do),而 URL 是受限页面。但是,当我 运行 浏览器上的场景时,我能够看到页面被重定向到访问被拒绝的页面。但是,当我 运行 与 htmlunit 相同时,我看到了以下异常。

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)

知道如何实现吗?这是我的代码。

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

成功了。抛出异常,因此它不允许页面遵循重定向。我将 WebClient 上的 属性 throwExceptionOnFailingStatusCode 设置为 false,以便忽略异常。

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