Apache HttpClient临时错误:NoHttpResponseException [英] Apache HttpClient Interim Error: NoHttpResponseException

查看:317
本文介绍了Apache HttpClient临时错误:NoHttpResponseException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Web服务,它接受带有XML的POST方法。它工作正常,然后在一些随机的场合,它无法与服务器通信抛出IOException与消息目标服务器无法响应。后续调用工作正常。

I have a webservice which is accepting a POST method with XML. It is working fine then at some random occasion, it fails to communicate to the server throwing IOException with message The target server failed to respond. The subsequent calls work fine.

当我拨打电话然后让我的应用程序闲置10-15分钟时,它主要发生。我之后的第一个调用返回此错误。

It happens mostly, when i make some calls and then leave my application idle for like 10-15 min. the first call which I make after that returns this error.

我尝试了几件事......

I tried couple of things ...

我设置了重试处理程序,如

I setup the retry handler like

HttpRequestRetryHandler retryHandler = new HttpRequestRetryHandler() {

            public boolean retryRequest(IOException e, int retryCount, HttpContext httpCtx) {
                if (retryCount >= 3){
                    Logger.warn(CALLER, "Maximum tries reached, exception would be thrown to outer block");
                    return false;
                }
                if (e instanceof org.apache.http.NoHttpResponseException){
                    Logger.warn(CALLER, "No response from server on "+retryCount+" call");
                    return true;
                }
                return false;
            }
        };

        httpPost.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, retryHandler);

但这次重试从未被调用过。 (是的,我正在使用right instanceof子句)。虽然调试这个类永远不会被调用。

but this retry never got called. (yes I am using right instanceof clause). While debugging this class never being called.

我甚至尝试设置 HttpProtocolParams.setUseExpectContinue(httpClient.getParams(),false); 但没有用。有人可以建议我现在能做什么吗?

I even tried setting up HttpProtocolParams.setUseExpectContinue(httpClient.getParams(), false); but no use. Can someone suggest what I can do now?

重要
除了弄明白为什么我得到例外之外,我有一个重要的问题是为什么不是retryhandler在这里工作?

IMPORTANT Besides figuring out why I am getting the exception, one of the important concerns I have is why isn't the retryhandler working here?

推荐答案

连接管理器保持活动状态的持久性连接很可能变得陈旧。也就是说,目标服务器在其端部关闭连接,而HttpClient无法对该事件作出反应,同时连接处于空闲状态,从而使连接半关闭或陈旧。通常这不是问题。 HttpClient采用多种技术来验证从池中租用的连接有效性。即使禁用过时连接检查并且使用过时连接来传输请求消息,请求执行通常在使用SocketException的写操作中失败并自动重试。但是在某些情况下,写操作可以在没有异常的情况下终止,随后的读操作返回-1(流结束)。在这种情况下,HttpClient别无选择,只能假设请求成功但服务器很可能由于服务器端的意外错误而无法响应。

Most likely persistent connections that are kept alive by the connection manager become stale. That is, the target server shuts down the connection on its end without HttpClient being able to react to that event, while the connection is being idle, thus rendering the connection half-closed or 'stale'. Usually this is not a problem. HttpClient employs several techniques to verify connection validity upon its lease from the pool. Even if the stale connection check is disabled and a stale connection is used to transmit a request message the request execution usually fails in the write operation with SocketException and gets automatically retried. However under some circumstances the write operation can terminate without an exception and the subsequent read operation returns -1 (end of stream). In this case HttpClient has no other choice but to assume the request succeeded but the server failed to respond most likely due to an unexpected error on the server side.

最简单的方法解决这种情况的方法是,在一段时间不活动之后,驱逐已经过时的连接和连接,这些连接和连接比池中的1分钟更长。有关详细信息,请参阅 HttpClient教程的这一部分

The simplest way to remedy the situation is to evict expired connections and connections that have been idle longer than, say, 1 minute from the pool after a period of inactivity. For details please see this section of the HttpClient tutorial.

这篇关于Apache HttpClient临时错误:NoHttpResponseException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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