如何解决Java HtmlUnit - 如何增加加载页面的超时时间?
我正在尝试加载一个页面,该页面通过“单击”这样的按钮来加载:
page2.getElementById("btn_6").click();
服务器需要大量时间来加载该页面,因为它需要进行一些计算。我当时面临的问题是我从 HtmlUnit 得到这个异常:
======= EXCEPTION START ========
Exception class=[java.lang.RuntimeException]
com.gargoylesoftware.htmlunit.ScriptException: Exception invoking submit
at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine$HtmlUnitContextAction.run(JavaScriptEngine.java:949)
at net.sourceforge.htmlunit.corejs.javascript.Context.call(Context.java:650)
at net.sourceforge.htmlunit.corejs.javascript.ContextFactory.call(ContextFactory.java:540)
at com.gargoylesoftware.htmlunit.javascript.HtmlUnitContextFactory.callSecured(HtmlUnitContextFactory.java:353)
at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.callFunction(JavaScriptEngine.java:878)
at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.callFunction(JavaScriptEngine.java:850)
at com.gargoylesoftware.htmlunit.html.HtmlPage.executeJavaScriptFunction(HtmlPage.java:2589)
at com.gargoylesoftware.htmlunit.html.HtmlPage.executeJavaScriptFunction(HtmlPage.java:2582)
at com.gargoylesoftware.htmlunit.javascript.host.event.EventListenersContainer.executeEventListeners(EventListenersContainer.java:342)
at com.gargoylesoftware.htmlunit.javascript.host.event.EventListenersContainer.executeAtTargetListeners(EventListenersContainer.java:379)
at com.gargoylesoftware.htmlunit.javascript.host.event.EventTarget.fireEvent(EventTarget.java:173)
at com.gargoylesoftware.htmlunit.html.DomElement.lambda$fireEvent$0(DomElement.java:1475)
at net.sourceforge.htmlunit.corejs.javascript.Context.call(Context.java:650)
at net.sourceforge.htmlunit.corejs.javascript.ContextFactory.call(ContextFactory.java:540)
at com.gargoylesoftware.htmlunit.javascript.HtmlUnitContextFactory.callSecured(HtmlUnitContextFactory.java:353)
at com.gargoylesoftware.htmlunit.html.DomElement.fireEvent(DomElement.java:1475)
at com.gargoylesoftware.htmlunit.html.DomElement.doClickFireClickEvent(DomElement.java:1143)
at com.gargoylesoftware.htmlunit.html.DomElement.click(DomElement.java:1074)
at com.gargoylesoftware.htmlunit.html.DomElement.click(DomElement.java:1014)
at com.gargoylesoftware.htmlunit.html.DomElement.click(DomElement.java:910)
at com.gargoylesoftware.htmlunit.html.DomElement.click(DomElement.java:887)
at com.gargoylesoftware.htmlunit.html.DomElement.click(DomElement.java:864)
at <path to method in "main">(Main.java:80)
at <path to "main" method>(Main.java:27)
Caused by: java.lang.RuntimeException: Exception invoking submit
at net.sourceforge.htmlunit.corejs.javascript.MemberBox.invoke(MemberBox.java:254)
at net.sourceforge.htmlunit.corejs.javascript.FunctionObject.call(FunctionObject.java:456)
at net.sourceforge.htmlunit.corejs.javascript.Interpreter.interpretLoop(Interpreter.java:1698)
at net.sourceforge.htmlunit.corejs.javascript.Interpreter.interpret(Interpreter.java:1013)
at net.sourceforge.htmlunit.corejs.javascript.InterpretedFunction.call(InterpretedFunction.java:111)
at com.gargoylesoftware.htmlunit.javascript.host.event.EventHandler.call(EventHandler.java:70)
at net.sourceforge.htmlunit.corejs.javascript.ContextFactory.doTopCall(ContextFactory.java:430)
at com.gargoylesoftware.htmlunit.javascript.HtmlUnitContextFactory.doTopCall(HtmlUnitContextFactory.java:339)
at net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime.doTopCall(ScriptRuntime.java:3665)
at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine$3.doRun(JavaScriptEngine.java:871)
at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine$HtmlUnitContextAction.run(JavaScriptEngine.java:934)
... 23 more
Caused by: java.lang.RuntimeException: java.net.socketTimeoutException: Read timed out
at com.gargoylesoftware.htmlunit.WebClient.download(WebClient.java:2418)
at com.gargoylesoftware.htmlunit.javascript.host.html.HTMLFormElement.submit(HTMLFormElement.java:340)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at net.sourceforge.htmlunit.corejs.javascript.MemberBox.invoke(MemberBox.java:228)
... 33 more
Caused by: java.net.socketTimeoutException: Read timed out
at java.base/java.net.socketInputStream.socketRead0(Native Method)
at java.base/java.net.socketInputStream.socketRead(SocketInputStream.java:115)
at java.base/java.net.socketInputStream.read(SocketInputStream.java:168)
at java.base/java.net.socketInputStream.read(SocketInputStream.java:140)
at java.base/sun.security.ssl.SSLSocketInputRecord.read(SSLSocketInputRecord.java:476)
at java.base/sun.security.ssl.SSLSocketInputRecord.readHeader(SSLSocketInputRecord.java:470)
at java.base/sun.security.ssl.SSLSocketInputRecord.bytesInCompletePacket(SSLSocketInputRecord.java:70)
at java.base/sun.security.ssl.SSLSocketImpl.readApplicationRecord(SSLSocketImpl.java:1308)
at java.base/sun.security.ssl.SSLSocketImpl$AppInputStream.read(SSLSocketImpl.java:949)
at org.apache.http.impl.io.SessionInputBufferImpl.streamRead(SessionInputBufferImpl.java:137)
at org.apache.http.impl.io.SessionInputBufferImpl.fillBuffer(SessionInputBufferImpl.java:153)
at org.apache.http.impl.io.SessionInputBufferImpl.readLine(SessionInputBufferImpl.java:280)
at org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:138)
at org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:56)
at org.apache.http.impl.io.AbstractMessageParser.parse(AbstractMessageParser.java:259)
at org.apache.http.impl.DefaultBHttpClientConnection.receiveResponseHeader(DefaultBHttpClientConnection.java:163)
at org.apache.http.impl.conn.CPoolProxy.receiveResponseHeader(CPoolProxy.java:157)
at org.apache.http.protocol.HttpRequestExecutor.doReceiveResponse(HttpRequestExecutor.java:273)
at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:125)
at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:272)
at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:186)
at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:89)
at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:110)
at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:185)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:72)
at com.gargoylesoftware.htmlunit.HttpWebConnection.getResponse(HttpWebConnection.java:192)
at com.gargoylesoftware.htmlunit.WebClient.loadWebResponseFromWebConnection(WebClient.java:1554)
at com.gargoylesoftware.htmlunit.WebClient.loadWebResponse(WebClient.java:1477)
at com.gargoylesoftware.htmlunit.WebClient.download(WebClient.java:2409)
... 39 more
Enclosed exception:
java.lang.RuntimeException: Exception invoking submit
at net.sourceforge.htmlunit.corejs.javascript.MemberBox.invoke(MemberBox.java:254)
at net.sourceforge.htmlunit.corejs.javascript.FunctionObject.call(FunctionObject.java:456)
at net.sourceforge.htmlunit.corejs.javascript.Interpreter.interpretLoop(Interpreter.java:1698)
at net.sourceforge.htmlunit.corejs.javascript.Interpreter.interpret(Interpreter.java:1013)
at net.sourceforge.htmlunit.corejs.javascript.InterpretedFunction.call(InterpretedFunction.java:111)
at com.gargoylesoftware.htmlunit.javascript.host.event.EventHandler.call(EventHandler.java:70)
at net.sourceforge.htmlunit.corejs.javascript.ContextFactory.doTopCall(ContextFactory.java:430)
at com.gargoylesoftware.htmlunit.javascript.HtmlUnitContextFactory.doTopCall(HtmlUnitContextFactory.java:339)
at net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime.doTopCall(ScriptRuntime.java:3665)
at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine$3.doRun(JavaScriptEngine.java:871)
at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine$HtmlUnitContextAction.run(JavaScriptEngine.java:934)
at net.sourceforge.htmlunit.corejs.javascript.Context.call(Context.java:650)
at net.sourceforge.htmlunit.corejs.javascript.ContextFactory.call(ContextFactory.java:540)
at com.gargoylesoftware.htmlunit.javascript.HtmlUnitContextFactory.callSecured(HtmlUnitContextFactory.java:353)
at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.callFunction(JavaScriptEngine.java:878)
at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.callFunction(JavaScriptEngine.java:850)
at com.gargoylesoftware.htmlunit.html.HtmlPage.executeJavaScriptFunction(HtmlPage.java:2589)
at com.gargoylesoftware.htmlunit.html.HtmlPage.executeJavaScriptFunction(HtmlPage.java:2582)
at com.gargoylesoftware.htmlunit.javascript.host.event.EventListenersContainer.executeEventListeners(EventListenersContainer.java:342)
at com.gargoylesoftware.htmlunit.javascript.host.event.EventListenersContainer.executeAtTargetListeners(EventListenersContainer.java:379)
at com.gargoylesoftware.htmlunit.javascript.host.event.EventTarget.fireEvent(EventTarget.java:173)
at com.gargoylesoftware.htmlunit.html.DomElement.lambda$fireEvent$0(DomElement.java:1475)
at net.sourceforge.htmlunit.corejs.javascript.Context.call(Context.java:650)
at net.sourceforge.htmlunit.corejs.javascript.ContextFactory.call(ContextFactory.java:540)
at com.gargoylesoftware.htmlunit.javascript.HtmlUnitContextFactory.callSecured(HtmlUnitContextFactory.java:353)
at com.gargoylesoftware.htmlunit.html.DomElement.fireEvent(DomElement.java:1475)
at com.gargoylesoftware.htmlunit.html.DomElement.doClickFireClickEvent(DomElement.java:1143)
at com.gargoylesoftware.htmlunit.html.DomElement.click(DomElement.java:1074)
at com.gargoylesoftware.htmlunit.html.DomElement.click(DomElement.java:1014)
at com.gargoylesoftware.htmlunit.html.DomElement.click(DomElement.java:910)
at com.gargoylesoftware.htmlunit.html.DomElement.click(DomElement.java:887)
at com.gargoylesoftware.htmlunit.html.DomElement.click(DomElement.java:864)
at <path to method in "main">(Main.java:80)
at <path to "main" method>(Main.java:27)
Caused by: java.lang.RuntimeException: java.net.socketTimeoutException: Read timed out
at com.gargoylesoftware.htmlunit.WebClient.download(WebClient.java:2418)
at com.gargoylesoftware.htmlunit.javascript.host.html.HTMLFormElement.submit(HTMLFormElement.java:340)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at net.sourceforge.htmlunit.corejs.javascript.MemberBox.invoke(MemberBox.java:228)
... 33 more
Caused by: java.net.socketTimeoutException: Read timed out
at java.base/java.net.socketInputStream.socketRead0(Native Method)
at java.base/java.net.socketInputStream.socketRead(SocketInputStream.java:115)
at java.base/java.net.socketInputStream.read(SocketInputStream.java:168)
at java.base/java.net.socketInputStream.read(SocketInputStream.java:140)
at java.base/sun.security.ssl.SSLSocketInputRecord.read(SSLSocketInputRecord.java:476)
at java.base/sun.security.ssl.SSLSocketInputRecord.readHeader(SSLSocketInputRecord.java:470)
at java.base/sun.security.ssl.SSLSocketInputRecord.bytesInCompletePacket(SSLSocketInputRecord.java:70)
at java.base/sun.security.ssl.SSLSocketImpl.readApplicationRecord(SSLSocketImpl.java:1308)
at java.base/sun.security.ssl.SSLSocketImpl$AppInputStream.read(SSLSocketImpl.java:949)
at org.apache.http.impl.io.SessionInputBufferImpl.streamRead(SessionInputBufferImpl.java:137)
at org.apache.http.impl.io.SessionInputBufferImpl.fillBuffer(SessionInputBufferImpl.java:153)
at org.apache.http.impl.io.SessionInputBufferImpl.readLine(SessionInputBufferImpl.java:280)
at org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:138)
at org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:56)
at org.apache.http.impl.io.AbstractMessageParser.parse(AbstractMessageParser.java:259)
at org.apache.http.impl.DefaultBHttpClientConnection.receiveResponseHeader(DefaultBHttpClientConnection.java:163)
at org.apache.http.impl.conn.CPoolProxy.receiveResponseHeader(CPoolProxy.java:157)
at org.apache.http.protocol.HttpRequestExecutor.doReceiveResponse(HttpRequestExecutor.java:273)
at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:125)
at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:272)
at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:186)
at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:89)
at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:110)
at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:185)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:72)
at com.gargoylesoftware.htmlunit.HttpWebConnection.getResponse(HttpWebConnection.java:192)
at com.gargoylesoftware.htmlunit.WebClient.loadWebResponseFromWebConnection(WebClient.java:1554)
at com.gargoylesoftware.htmlunit.WebClient.loadWebResponse(WebClient.java:1477)
at com.gargoylesoftware.htmlunit.WebClient.download(WebClient.java:2409)
... 39 more
== CALLING JAVASCRIPT ==
function () {
[native code]
}
======= EXCEPTION END ========
有没有办法让 HtmlUnit 的超时设置为 5 分钟之类的?
问候!
解决方法
找到了。
是:
webClient.getOptions().setTimeout(5*60*1000);
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。