欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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

程序员文章站 2024-03-23 10:06:16
...

前几天在项目联调中遇到接口报错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)

相关标签: HTTP Client