lrpowell · GitHub

Exception in thread "main" java.lang.ClassCastException: com.gargoylesoftware.htmlunit.UnexpectedPage cannot be cast to com.gargoylesoftware.htmlunit.html.HtmlPage
at com.ibm.as400.pt.viewer.script.loginNav.doLogin(loginNav.java:232)
at com.ibm.as400.pt.viewer.script.loginNav.main(loginNav.java:108)

try (final WebClient webClient = new WebClient(BrowserVersion.FIREFOX)) {
   java.util.logging.Logger.getLogger("com.gargoylesoftware").setLevel(java.util.logging.Level.OFF);
				webClient.getCache().clear();
				webClient.getOptions().setCssEnabled(true);
				webClient.getOptions().setJavaScriptEnabled(true);
				webClient.setAjaxController(new NicelyResynchronizingAjaxController());
				webClient.getOptions().setThrowExceptionOnScriptError(true);
				webClient.waitForBackgroundJavaScriptStartingBefore(10_000);
				URL thisUrl = new URL("http://localhost:9080/Navigator/DispatcherServlet/login");
				WebRequest requestSettings = new WebRequest(thisUrl, HttpMethod.POST);
			    requestSettings.setAdditionalHeader("Content-Type", "application/json");
			    requestSettings.setRequestBody("{'name':'abc123', 'password':'xxxyyyzz'}");
			    System.out.println("request:"+requestSettings.toString());
				**Page redirectPage = webClient.getPage(requestSettings);**
// above line will get the classCastException if class HtmlPage is used instead of Page
				WebResponse response = redirectPage.getWebResponse();
				System.out.println("responseHeaders:"+response.getResponseHeaders());
				**System.out.println("PAGE"+(HtmlPage)redirectPage).asText());**. // Error in Eclipse: Cannot invoke "asText" on the primitive type void
232				HtmlPage htmlPage = (HtmlPage) redirectPage;	 // runtime error
				String content = response.getContentAsString();
				System.out.println("content:"+content);
				DomElement enterButton = htmlPage.getElementById("loginbutton");
				DomElement usernameField = htmlPage.getElementById("loginUsername");
				DomElement passwordField = htmlPage.getElementById("password");
				usernameField.setAttribute("value", m_userProfile);
				passwordField.setAttribute("value", m_userPW);
				Page loggedInPage = enterButton.click();
				webClient.waitForBackgroundJavaScript(60000);
				boolean hasPage = ((HtmlPage) loggedInPage).asText().contains("IBM Navigator for i");
				System.out.println("@new page:");
				 System.out.println( ((HtmlPage)loggedInPage).asText());
				System.out.println("@end page");

Debug output:

request:WebRequest[<url="http://localhost:9080/Navigator/DispatcherServlet/login", POST, EncodingType[name=application/x-www-form-urlencoded], [], {Accept=/, Accept-Encoding=gzip, deflate, Content-Type=application/json}, null>]
responseHeaders:
[X-Powered-By=Servlet/4.0, Content-Type=application/json; charset=UTF-8, Content-Language=en-US, Set-Cookie=JSESSIONID=0000c1RnbEv5FkPQngDPb698wmX:b2fa7a58-1594-49c6-a8b9-e5a074461081; Path=/; HttpOnly, Transfer-Encoding=chunked, Date=Thu, 04 Feb 2021 16:43:25 GMT, Expires=Thu, 01 Dec 1994 16:00:00 GMT, Cache-Control=no-cache="set-cookie, set-cookie2"]

When I used the incorrect URL and get response back that the login failed, I do not get the runtime error:

request:WebRequest[<url="http://localhost:9080/Navigator/login", POST, EncodingType[name=application/x-www-form-urlencoded], [], {Accept=/, Accept-Encoding=gzip, deflate, Content-Type=application/json}, null>]
responseHeaders:[X-Powered-By=Servlet/4.0, Date=Thu, 04 Feb 2021 16:47:25 GMT, Content-Type=text/html, Last-Modified=Wed, 27 Jan 2021 19:07:30 GMT, Content-Length=1469, Content-Language=en-US]

Read the original on github.com ↗