如何在HtmlUnit中使用Websockets超时页面

时间:2015-07-26 03:39:57

标签: java websocket htmlunit htmlunit-driver

示例网址:http://www.heatpressnation.com/sublimation-printers-ink.html
Websocket URL:https://us22.zopim.com

当调用该页面时,由于与zopim.com的websocket连接,HtmlUnit永远不会完成。如何在设置超时后强制页面完成或识别Web套接字并关闭它。这是我的代码的主要内容:

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());
} 

0 个答案:

没有答案