2

我希望,在连接重置后不会再次执行 HtmlUnit 中的 Web 请求。以下异常消息显示重试连接:

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

那么如何在 HtmlUnit (java) 中禁用或指定重试次数?

4

1 回答 1

2

由于类的结构,我一直在处理这个问题。我不是 Java 专家,但它可能会对您有所帮助,因为我在 Google 上搜索了几天无济于事。我不确定我们是否遇到同样的问题,但我在集成测试中偶尔会遇到“org.apache.http.NoHttpResponseException:目标服务器无法响应”,并且正在对其进行测试,希望它能解决.

这可能是一种不好的方法,并且没有涵盖所有入口点,但也许它对您有用。

使用名为 RetryHttpWebConnection 的类子类化 HttpWebConnection 并添加此覆盖:

@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;
}

子类 WebClient 并在子类的构造函数中执行以下操作:

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

编辑:我也在研究http://pastebin.com/nmmRYqKN,这可能是我对特定例外所需要的更多。

于 2012-09-28T03:20:02.640 回答