這是爲我的工作項目,所以我不能像我想要的那樣具體。HtmlUnit FailingHttpStatusCodeException:400錯誤的請求
的Java應用程序我設計應該做到以下幾點:
- 填補了網頁的形式,
- 點擊了提交
- 打開顯示成功/失敗 了新的一頁
- 在5秒內,新頁面根據成功/失敗自動重定向到另一個頁面
- 數據庫因此被更新。
(網頁已經存在,是因爲我在這裏所描述的,和我的應用程序是假設自動處理它們設置)
我嘗試可以使用HtmlUnit來處理網頁,形式和點擊填充鈕釦。
但是我收到一些例外,我希望儘可能避免。
1)如果我在配置Web客戶端,像這樣:
wc = new WebClient();
wc.setJavaScriptEnabled(false);
的代碼做的事情是應該的,但引發此異常:
dec 09, 2015 5:28:37 PM com.gargoylesoftware.htmlunit.WebClient printContentIfNecessary
INFO: statusCode=[400] contentType=[]
dec 09, 2015 5:28:37 PM com.gargoylesoftware.htmlunit.WebClient printContentIfNecessary
INFO:
com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException: 400 Bad Request for http://www.booking.com/flexiproduct.html
at com.gargoylesoftware.htmlunit.WebClient.throwFailingHttpStatusCodeExceptionIfNecessary(WebClient.java:540)
at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:360)
at com.gargoylesoftware.htmlunit.html.BaseFrame.loadInnerPageIfPossible(BaseFrame.java:136)
at com.gargoylesoftware.htmlunit.html.BaseFrame.loadInnerPage(BaseFrame.java:109)
at com.gargoylesoftware.htmlunit.html.HtmlPage.loadFrames(HtmlPage.java:1527)
at com.gargoylesoftware.htmlunit.html.HtmlPage.initialize(HtmlPage.java:138)
at com.gargoylesoftware.htmlunit.WebClient.loadWebResponseInto(WebClient.java:461)
at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:359)
at com.gargoylesoftware.htmlunit.ImmediateRefreshHandler.handleRefresh(ImmediateRefreshHandler.java:83)
at com.gargoylesoftware.htmlunit.html.HtmlPage.executeRefreshIfNeeded(HtmlPage.java:1093)
at com.gargoylesoftware.htmlunit.html.HtmlPage.initialize(HtmlPage.java:145)
at com.gargoylesoftware.htmlunit.WebClient.loadWebResponseInto(WebClient.java:461)
at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:359)
at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:382)
at com.gargoylesoftware.htmlunit.html.HtmlForm.submit(HtmlForm.java:242)
at com.gargoylesoftware.htmlunit.html.HtmlImageInput.doClickAction(HtmlImageInput.java:150)
at com.gargoylesoftware.htmlunit.html.ClickableElement.click(ClickableElement.java:148)
at com.gargoylesoftware.htmlunit.html.ClickableElement.click(ClickableElement.java:109)
at com.gargoylesoftware.htmlunit.html.ClickableElement.click(ClickableElement.java:87)
at com.gargoylesoftware.htmlunit.html.HtmlImageInput.click(HtmlImageInput.java:175)
at com.gargoylesoftware.htmlunit.html.HtmlImageInput.click(HtmlImageInput.java:132)
at paymentproviders.PProvider.submitForm(PProvider.java:52)
at paymentproviders.APXProvider.completeTransaction(APXProvider.java:114)
at workApps.Lazaruss1.App.main(App.java:80)
代碼工作,數據庫已成功更新,但在最後一次重定向時失敗並且未打開最終頁面。 :(
2)當我配置的Web客戶端,例如:
wc.setThrowExceptionOnFailingStatusCode(false);
我得到下面的異常和程序不能正常工作:這點後
dec 09, 2015 5:31:12 PM com.gargoylesoftware.htmlunit.WebClient printContentIfNecessary
INFO: statusCode=[400] contentType=[]
dec 09, 2015 5:31:12 PM com.gargoylesoftware.htmlunit.WebClient printContentIfNecessary
INFO:
Failed to read the SOAP message.
java.lang.RuntimeException: Refresh Aborted by HtmlUnit: Attempted to refresh a page using an ImmediateRefreshHandler which could have caused an OutOfMemoryError Please use WaitingRefreshHandler or ThreadedRefreshHandler instead.
at com.gargoylesoftware.htmlunit.ImmediateRefreshHandler.handleRefresh(ImmediateRefreshHandler.java:81)
at com.gargoylesoftware.htmlunit.html.HtmlPage.executeRefreshIfNeeded(HtmlPage.java:1093)
at com.gargoylesoftware.htmlunit.html.HtmlPage.initialize(HtmlPage.java:145)
at com.gargoylesoftware.htmlunit.WebClient.loadWebResponseInto(WebClient.java:461)
at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:359)
at com.gargoylesoftware.htmlunit.ImmediateRefreshHandler.handleRefresh(ImmediateRefreshHandler.java:83)
at com.gargoylesoftware.htmlunit.html.HtmlPage.executeRefreshIfNeeded(HtmlPage.java:1093)
at com.gargoylesoftware.htmlunit.html.HtmlPage.initialize(HtmlPage.java:145)
at com.gargoylesoftware.htmlunit.WebClient.loadWebResponseInto(WebClient.java:461)
at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:359)
at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:382)
at com.gargoylesoftware.htmlunit.html.HtmlForm.submit(HtmlForm.java:242)
at com.gargoylesoftware.htmlunit.html.HtmlImageInput.doClickAction(HtmlImageInput.java:150)
at com.gargoylesoftware.htmlunit.html.ClickableElement.click(ClickableElement.java:148)
at com.gargoylesoftware.htmlunit.html.ClickableElement.click(ClickableElement.java:109)
at com.gargoylesoftware.htmlunit.html.ClickableElement.click(ClickableElement.java:87)
at com.gargoylesoftware.htmlunit.html.HtmlImageInput.click(HtmlImageInput.java:175)
at com.gargoylesoftware.htmlunit.html.HtmlImageInput.click(HtmlImageInput.java:132)
at paymentproviders.PProvider.submitForm(PProvider.java:52)
at paymentproviders.APXProvider.completeTransaction(APXProvider.java:114)
at workApps.Lazaruss1.App.main(App.java:80)
的代碼和退出數據庫不會更改。
我想要的是完全消除這些例外。我如何配置WebClient停止像這樣的混蛋?
更新 - 提交按鈕不是,而是一個圖像。我點擊它使用下面的代碼: – Lazaruss
page =(HtmlPage)((ClickableElement)form.getFirstByXPath(nameValues.get(「submitXPath」)))。click(); – Lazaruss