WebClient异步请求
WebClient异步请求
public String getHtmlPageResponse(String url) throws Exception { //请求超时时间,默认20秒 int timeout = 20000; //等待异步JS执行时间,默认20秒 int waitForBackgroundJavaScript = 20000; String result = ""; final WebClient webClient = new WebClient(BrowserVersion.CHROME); webClient.getOptions().setThrowExceptionOnScriptError(false);//当JS执行出错的时候是否抛出异常 webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);//当HTTP的状态非200时是否抛出异常 webClient.getOptions().setActiveXNative(false); webClient.getOptions().setCssEnabled(false);//是否启用CSS webClient.getOptions().setJavaScriptEnabled(true); //很重要,启用JS webClient.setCssErrorHandler(new SilentCssErrorHandler()); webClient.setJavaScriptErrorListener(new JavaScriptErrorListener(){ public void scriptException(HtmlPage page, ScriptException scriptException) { // TODO Auto-generated method stub System.out.println("scriptException"); } public void timeoutError(HtmlPage page, long allowedTime, long executionTime) { // TODO Auto-generated method stub System.out.println("timeoutError"); } public void malformedScriptURL(HtmlPage page, String url, MalformedURLException malformedURLException) { // TODO Auto-generated method stub System.out.println("malformedScriptURL"); } public void loadScriptError(HtmlPage page, URL scriptUrl, Exception exception) { // TODO Auto-generated method stub System.out.println("loadScriptError"); } public void warn(String message, String sourceName, int line, String lineSource, int lineOffset) { // TODO Auto-generated method stub System.out.println("warn"); }}); webClient.setAjaxController(new NicelyResynchronizingAjaxController());//很重要,设置支持AJAX //webClient.getOptions().setThrowExceptionOnScriptError(false); webClient.getOptions().setTimeout(timeout);//设置“浏览器”的请求超时时间 webClient.setJavaScriptTimeout(timeout);//设置JS执行的超时时间 HtmlPage page; try { page = webClient.getPage(url); } catch (Exception e) { webClient.close(); throw e; } webClient.waitForBackgroundJavaScript(waitForBackgroundJavaScript);//该方法阻塞线程 result = page.asXml(); webClient.close(); return result; }
##############
QQ 3087438119