2015-12-10 49 views
0

這是爲我的工作項目,所以我不能像我想要的那樣具體。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停止像這樣的混蛋?

+0

更新 - 提交按鈕不是,而是一個圖像。我點擊它使用下面的代碼: – Lazaruss

+0

page =(HtmlPage)((ClickableElement)form.getFirstByXPath(nameValues.get(「submitXPath」)))。click(); – Lazaruss

回答

0

更多信息:

這是初始頁的形式:

<form id="paymentPageData" action="processConfirmPaymentPage" method="post"> 

        <div style="display: none;"> 
         <input id="transactionId" name="tranId" type="hidden" value="1510351"/> 
         <input id="cashAmount" name="amount" type="hidden" value="5.43"/> 
         <input id="usedCurrency" name="currency" type="hidden" value="EUR"/> 
         <input id="merchantRef" name="merchantTxRef" type="hidden" value="83002679818"/> 
         <input id="digestKey" name="digest" type="hidden" value="aa76e7f0db12ddbe86c1d3e13fd512e70ccfa535fbd93a347cbe2847c6187aca"/> 
         <input id="cssUrl" name="cssUrl" type="hidden" value=""/> 
        </div> 

        <div> 
         <input type="image" name="confirm" src="common/images/confirm-ukash_en_GB.jpg" border="0"> 
        </div> 
        <div> 
         <input type="image" name="reverse" src="common/images/cancel-ukash_en_GB.jpg" border="0"> 
        </div> 

       </form> 

這是一個成功的按鈕打開頁面:

<link rel="stylesheet" type="text/css" href=""> 


    <meta http-equiv="refresh" content="3;url=http://www.b92.net?Operation.ResultMessage=Success&amp;DigestKey=05980b62080143b0e9c1d56b304425cde3bd4acd1277c759bad007fb5eba5f95&amp;Merch.ID=78700005&amp;Processor.Response=00&amp;Account.Num=00000007&amp;Tran.Ref=83002679818&amp;OperationResult=SUCCESS&amp;Operation.Currency=EUR&amp;Operation.Amount=543&amp;Operation.Date=2015-12-10T10:29:26Z&amp;Transaction.Auth=&amp;Merchant.TxReference=83002679818"> 

</head> 

<body> 
    <div id="page"> 
     <div id="redirectPage"> 
      <div id="successfulRedirectPage"> 
       <div id="header"></div> 
       <div id="left"></div> 
       <div id="centre"> 
        <div id="successfulHeader"> 
         Your payment was successful 
        </div> 
        <div id="successfulMessage"> 
         You will shortly be redirected back to the original site. Or, simply click the link below. 
        </div> 
        <div id="successfulLink"> 

          <a href="http://www.b92.net?Operation.ResultMessage=Success&amp;DigestKey=05980b62080143b0e9c1d56b304425cde3bd4acd1277c759bad007fb5eba5f95&amp;Merch.ID=78700005&amp;Processor.Response=00&amp;Account.Num=00000007&amp;Tran.Ref=83002679818&amp;OperationResult=SUCCESS&amp;Operation.Currency=EUR&amp;Operation.Amount=543&amp;Operation.Date=2015-12-10T10:29:26Z&amp;Transaction.Auth=&amp;Merchant.TxReference=83002679818">Return</a> 

        </div> 
       </div> 
       <div id="right"></div> 
       <div id="footer"></div> 
      </div> 
     </div> 
    </div> 
</body> 

(我已經改變了性質名稱,以保護我的僱主的隱私)

+0

我認爲關閉我的webclient中的JavaScript是阻止這種重定向的原因。但是如果我放棄它,我會得到一個巨大的異常,這太大而不適合在控制檯中。 – Lazaruss

0

我解決了這個問題:)

刷新導致的頁面過於龐大,無法放入頁面對象。

我改變了重定向的URL:https://login.live.com/

而且現在的工作。