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

时间:2011-02-06 19:52:53

标签: java timeout htmlunit

我遇到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);
}

2 个答案:

答案 0 :(得分:3)

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

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

再见

答案 1 :(得分:0)

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

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