从htmlunit WebClient调用getPage,禁用JavaScript并将setTimeout设置为10000等待

我遇到了Htmlunit的问题,我在调用getpage之前禁用了JavaScript并将超时设置为10000,我预计超时后会出现exception,但htmlunit会永远等待。

经过一番搜索,我发现2009年有人遇到了同样的问题( 连接超时无法正常工作 ),他抱怨“连接超时无法正常工作”,并且关于超时中的某些值无法正常工作,但直到2011年才得到任何答案。

这里有人询问抛出了什么exception,但我认为它并不总是抛出exception。 我也无法从Apache HttpClient setTimeout得到答案。 你可以看到另一个人在终止或停止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; } }); 

再见

我发现,用HttpUnit 1.6.2设置这些

  final HttpClient client = new HttpClient(); final GetMethod method = new GetMethod(pUrl); client.setConnectionTimeout((int) timeout); client.setTimeout((int) timeout); final int statusCode = client.executeMethod(method); 

似乎要做的伎俩。 两者都是弃用的方法。 🙁