user1375468
user1375468

Reputation: 21

How can I disable or specify the number of retries in HtmlUnit (java)?

I want, that a web request in HtmlUnit is not executed again after a connection reset.The following exception message shows the retrying connect:

INFO: I/O exception (java.net.SocketException) caught when connecting to the target host: Connection reset
* * org.apache.http.impl.client.DefaultRequestDirector tryConnect
INFO: Retrying connect

So how can I disable or specify the number of retries in HtmlUnit (java)?

Upvotes: 2

Views: 1568

Answers (1)

Rob Barreca
Rob Barreca

Reputation: 649

Because of how the classes are structured, I have been working with this. I'm no Java expert, but it might help you as I Googled for days to no avail. I'm unsure if we're having the same problem, but I'm getting the occasional 'org.apache.http.NoHttpResponseException: The target server failed to respond' in my integration tests and was testing this out in hopes it would fix.

It's likely a bad approach and doesn't cover all the entry points, but maybe it will work for you.

Subclass HttpWebConnection with a class called RetryHttpWebConnection and add this override:

@Override
protected AbstractHttpClient createHttpClient() {
    AbstractHttpClient client = super.createHttpClient();

    // Set it to do some retrying in case of closed connection during test.
    client.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
        new DefaultHttpMethodRetryHandler(5, false));

    return client;
}

Subclass WebClient and in the subclass's constructor do this:

// Override with new web connection that will do retries
// to avoid test failures due to network issues.
setWebConnection(new RetryHttpWebConnection(this));

EDIT: I'm also looking into http://pastebin.com/nmmRYqKN, which might be more what I need for my particular exception.

Upvotes: 2

Related Questions