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

假如想象 提交于 2019-12-11 03:11:49

问题


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)?


回答1:


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.



来源:https://stackoverflow.com/questions/10452910/how-can-i-disable-or-specify-the-number-of-retries-in-htmlunit-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!