org.apache.http.NoHttpResponseException: failed to respond 问题解决

前几天在项目联调中遇到接口报错org.apache.http.NoHttpResponseException:  failed to respond 。用postMan 等工具测试接口都正常,但是在应用中,就是报该错误,困扰了几天。后经过分析,我们应用中先调用了一个查询接口,紧接着又调用了其它接口,使用HttpClientUtils工具实现Http接口调用,用了HTTP链接池。由于对方提供的接口未使用HTTP连接池,导致我们还使用同一个Http链接,而对方已经断开了HTTP链接。网上找了很多资料,要求tomcat配置文件修改如下:

<Connector port="8090" protocol="HTTP/1.1"
               connectionTimeout="20000"
               keepAliveTimeout="10000"
               redirectPort="8443" />

增加 keepAliveTimeout="10000"。但是我们不是接口服务提供方,但这个配置给了指引,需修改HttpClient

增加策略KeepAliveStrategy

//DefaultConnectionKeepAliveStrategy 默认实现
ConnectionKeepAliveStrategy myStrategy = new ConnectionKeepAliveStrategy() {
    @Override
    public long getKeepAliveDuration(HttpResponse response, HttpContext context) {
        Args.notNull(response, "HTTP response");
        final HeaderElementIterator it = new BasicHeaderElementIterator(
                response.headerIterator(HTTP.CONN_KEEP_ALIVE));
        while (it.hasNext()) {
            final HeaderElement he = it.nextElement();
            final String param = he.getName();
            final String value = he.getValue();
            if (value != null && param.equalsIgnoreCase("timeout")) {
                try {
                    return Long.parseLong(value) * 1000;
                } catch (final NumberFormatException ignore) {
                }
            }
        }
        return 1;
    }

};
final CloseableHttpClient httpclient = HttpClients.custom().setConnectionManagerShared(true)
        .setConnectionManager(POOL)
        .setKeepAliveStrategy(myStrategy)
        .evictExpiredConnections()
        .build();

org.apache.http.NoHttpResponseException: X.X.X.X:8080 failed to respond
at org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:143)
at org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:57)
at org.apache.http.impl.io.AbstractMessageParser.parse(AbstractMessageParser.java:261)
at org.apache.http.impl.DefaultBHttpClientConnection.receiveResponseHeader(DefaultBHttpClientConnection.java:165)
at org.apache.http.impl.conn.CPoolProxy.receiveResponseHeader(CPoolProxy.java:167)
at org.apache.http.protocol.HttpRequestExecutor.doReceiveResponse(HttpRequestExecutor.java:272)
at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:124)
at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:271)
at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:184)
at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:88)
at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:110)
at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:184)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:107)

  • 12
    点赞
  • 40
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值