我尝试在登录应用程序后直接访问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);
答案 0 :(得分:2)
搞定了。抛出异常,因此它不允许页面遵循重定向。我将throwExceptionOnFailingStatusCode
上的属性WebClient
设置为false
,以便忽略该异常。
HtmlPage resp = null;
WebClient webClient = getWebClient();
webClient.setThrowExceptionOnFailingStatusCode(false);
HtmlPage page = (HtmlPage) webClient.getPage(url);