从禁用了JavaScript并且setTimeout设置为10000的htmlunit WebClient调用getPage永远等待
作者:互联网
我在使用Htmlunit时遇到问题,我在调用getpage之前禁用了JavaScript并将超时设置为10000,我希望超时后会发生异常,但htmlunit会一直等待.
经过一番搜索后,我意识到2009年有人遇到相同的问题(Connection timeout not working),他抱怨“连接超时不起作用”,并且超时中的某些值不起作用,但是直到2011年为止都没有得到任何答案.
有人here在询问引发了什么异常,但是我认为它并不总是引发异常.我也无法从Apache HttpClient setTimeout得到答案.您可以看到另一个人在Terminate or Stop HtmlUnit中询问超时暂停.
如果尝试以下操作,您会发现它有多疯狂:
milisecReqTimeout = 10;
while(true)
{
_webclient.setTimeout(milisecReqTimeout);
milisecReqTimeout = milisecReqTimeout + 10;
_htmlpage = _webclient.getPage(url);
}
解决方法:
_thewebclient.setWebConnection(new HttpWebConnection(_thewebclient) {
@Override
protected synchronized AbstractHttpClient getHttpClient() {
AbstractHttpClient client = super.getHttpClient();
if (_TimeoutCliSocket > 0) {
//Sets the socket timeout (SO_TIMEOUT) in milliseconds to
//be used when executing the method.
//A timeout value of zero is interpreted as an infinite timeout.
//Time that a read operation will block for, before generating
//an java.io.InterruptedIOException
client.getParams().setParameter("http.socket.timeout",
_TimeoutCliSocket);
}
if (_TimeoutCliConnection > 0) {
//The timeout in milliseconds used when retrieving an
// HTTP connection from the HTTP connection manager.
// Zero means to wait indefinitely.
client.getParams().setParameter("http.connection-manager.timeout",
_TimeoutCliConnection);
}
client.getParams().setParameter("http.tcp.nodelay", true);
return client;
}
});
再见
标签:timeout,htmlunit,java 来源: https://codeday.me/bug/20191208/2093772.html