Java 如何在HtmlUnit 2.9中禁用导致应用程序挂起的反向ajax轮询?
我正在使用基本的webClient.getPage方法在身份验证后检索页面,但该网站使用某种comet/meteor服务器来发出永无止境的ajax请求,因此getPage进入一个循环,我得到: 2012年6月22日下午3:40:15 com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl通知 警告:遇到过时的内容类型: “应用程序/x-javascript” 如果我一起禁用javascript,那么我将获得源页面,它将停止挂起:Java 如何在HtmlUnit 2.9中禁用导致应用程序挂起的反向ajax轮询?,java,htmlunit,Java,Htmlunit,我正在使用基本的webClient.getPage方法在身份验证后检索页面,但该网站使用某种comet/meteor服务器来发出永无止境的ajax请求,因此getPage进入一个循环,我得到: 2012年6月22日下午3:40:15 com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl通知 警告:遇到过时的内容类型: “应用程序/x-javascript” 如果我一起禁用javascript,那么我将获得源页面,它将停止挂起: webC
webClient.setJavaScriptEnabled(false);
但是,我不能使用HtmlUnit功能,比如单击包含javascript事件的按钮。
我想我不是第一个碰到这个问题的人,但我似乎找不到一个像样的解决办法
我尝试连接的页面是facebook,这是我的代码:
public static void submittingForm() throws Exception {
// final WebClient webClient = new WebClient();
final WebClient webClient = new WebClient(BrowserVersion.FIREFOX_3_6);
webClient.setJavaScriptEnabled(true);
webClient.setTimeout(60000);
webClient.setRedirectEnabled(true);
webClient.setThrowExceptionOnFailingStatusCode(false);
webClient.setThrowExceptionOnScriptError(false);
webClient.setCssEnabled(false);
webClient.setUseInsecureSSL(true);
// Get the first page
final HtmlPage page1 = webClient.getPage("http://www.facebook.com");
// Get the form that we are dealing with and within that form,
// find the submit button and the field that we want to change.
final HtmlForm form = page1.getHtmlElementById("login_form");
final HtmlTextInput textFieldUsername = form.getInputByName("email");
final HtmlPasswordInput textFieldPassword = form.getInputByName("pass");
final HtmlSubmitInput button = form.getInputByValue("Log In");
// Change the value of the text field
textFieldUsername.setValueAttribute("emailhere/username");
textFieldPassword.setValueAttribute("password here");
// Now submit the form by clicking the button and get back the second page.
// And get the cookie set up.
final HtmlPage page2= button.click();
//Go to the bob marley fan page
HtmlPage fanPage = webClient.getPage("http://www.facebook.com/BobMarley");
webClient.setJavaScriptEnabled(true);
// Get the label that containes the like button from the fan page
HtmlLabel likeLabel = fanPage.getHtmlElementById("timelineHeadlineLikeButton");
try{
// Get the like button
HtmlSubmitInput likeButton = (HtmlSubmitInput)likeLabel.getLastChild();
// Press it
likeButton.click();
} catch (Exception e){
e.printStackTrace();
}
webClient.closeAllWindows();
}
假设此线程仍处于打开状态且未解析状态。为了使可能遇到此问题的人受益: 我尝试使用HtmlUnit登录到一个内部网站(不向web开放)。它挂起,发出与您遇到的相同的消息
Oct 09, 2013 1:39:59 PM com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl notify
WARNING: Obsolete content type encountered: 'application/x-javascript'.
它进入了一个循环。这只是一个可以关闭的警告。脚本挂起的原因之一可能是因为它正在等待一些javascript加载
您已设置连接超时。尝试使用以下命令设置javascript超时:
webClient.setJavaScriptTimeout(45000); //Set JavaScript Timeout to 0.75 minute
我也这么做了,这对我很有效。它超时并继续执行剩余的代码行:
我得到了以下输出:
INFO: Caught script timeout error
com.gargoylesoftware.htmlunit.javascript.TimeoutError: Javascript execution takes too long (allowed: 45000, already elapsed: 45001)
................
如果无响应脚本对您的登录操作不是至关重要的,您的代码将正常工作。我还没有看到这种情况,在您的位置上,我将尝试使用最新的svn或构建它,这对我管理Buggy的javascripts有很大帮助。@maxmax不起作用,但非常感谢。这个url是公开的还是你知道另一个类似的?我很乐意help@maxmax我编辑了我的问题以包含代码。如果你能帮忙,我会很高兴的