由于类的结构,我一直在处理这个问题。我不是 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,这可能是我对特定例外所需要的更多。