如何在HtmlUnit(java)中禁用或指定重试次数? [英] How can I disable or specify the number of retries in HtmlUnit (java)?

查看:117
本文介绍了如何在HtmlUnit(java)中禁用或指定重试次数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

推荐答案

由于类的结构方式,我一直在努力.我不是Java专家,但是当我用Google搜索几天都无济于事时,它可能会对您有所帮助.我不确定是否遇到相同的问题,但是在集成测试中偶尔会遇到"org.apache.http.NoHttpResponseException:目标服务器无法响应"的情况,并且正在对此进行测试,希望可以解决

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.

使用名为RetryHttpWebConnection的类对HttpWebConnection进行子类化,并添加此替代项:

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

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

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

我也在研究 http://pastebin.com/nmmRYqKN ,可能更多我需要什么特殊的例外条件.

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

这篇关于如何在HtmlUnit(java)中禁用或指定重试次数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆