Menu ▾ ▴

#1703 How to Timeout Page Using Websockets in HtmlUnit

2.17
open
nobody
None
1
2015-07-26
2015-07-26
No

Websocket URL: https://us22.zopim.com

When calling that page, HtmlUnit never completes because of the websocket connection to zopim.com. How can I force the page to finish after a set timeout or identify the web socket and close it. Here is the meat of my code:

try{       
URL url = new URL(strUrl);
WebRequest webRequest = new WebRequest(url);
        WebClient webClient = new WebClient(BrowserVersion.FIREFOX_24);
        new WebConnectionWrapper(webClient) {
                public WebResponse getResponse(WebRequest request) throws IOException {
                        System.err.println("Url: " + request.getUrl());
                        WebResponse response = super.getResponse(request);
                        if(response.getContentType().equals("text/html") || response.getContentType().equals(null) || response.getContentType().equals("") || response.getContentType().equals("application/x-www-form-urlencoded"))
                        {
                                System.out.println(response.getWebRequest().getUrl());
                        }
                        return response;
                }
        };
        webClient.getOptions().setTimeout(10000);
        webClient.setJavaScriptTimeout(5000);
        webClient.setRefreshHandler(new ImmediateRefreshHandler());
        webClient.getPage(webRequest);
        webClient.waitForBackgroundJavaScript(3000);
}catch(Exception error){
        System.err.println("Error: " + error.toString());
} 

Discussion

  • Ahmed Ashour

    Ahmed Ashour - 2015-07-26
    • Please don't directly assign to individual members.
    • You don't need to cross post
     
  • Ahmed Ashour

    Ahmed Ashour - 2015-07-26
    • Description has changed:

    Diff:

    --- old
    +++ new
    @@ -2,6 +2,7 @@
    
     When calling that page, HtmlUnit never completes because of the websocket connection to zopim.com. How can I force the page to finish after a set timeout or identify the web socket and close it. Here is the meat of my code:
    
    +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     try{       
     URL url = new URL(strUrl);
     WebRequest webRequest = new WebRequest(url);
    @@ -26,3 +27,4 @@
             System.err.println("Error: " + error.toString());
     } 
    
    +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    • assigned_to: Ahmed Ashour --> nobody
     

Log in to post a comment.