【问题标题】:Call getPage from htmlunit WebClient with JavaScript disabled and setTimeout set to 10000 waits forever在禁用 JavaScript 并将 setTimeout 设置为 10000 的情况下从 htmlunit WebClient 调用 getPage 将永远等待
【发布时间】:2011-02-06 19:52:52
【问题描述】:

我在使用 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);
}

【问题讨论】:

标签: java timeout htmlunit


【解决方案1】:

     _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;
     }
 });

再见

【讨论】:

  • 哇这个好用!!经过这么长时间寻找答案!几乎放弃了。谢谢!
  • 我收回它。看起来它毕竟没有。线程仍然卡住。
【解决方案2】:

我发现,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);

似乎成功了。两者都是不推荐使用的方法。 :(

【讨论】:

  • 非常感谢,现在 00:19 在巴西这么晚了,但明天我会试试,没问题,它已被弃用,我希望将来更改 htmunit 层。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-01-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多