1

我尝试admin.do在登录应用程序后直接访问 URL (),并且该 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);
4

1 回答 1

4

得到它的工作。抛出异常,因此它不允许页面遵循重定向。我将属性设置throwExceptionOnFailingStatusCodeWebClientto false,以便忽略异常。

HtmlPage resp = null;
WebClient webClient = getWebClient();
webClient.setThrowExceptionOnFailingStatusCode(false);
HtmlPage page = (HtmlPage) webClient.getPage(url);
于 2016-05-27T10:58:17.690 回答