HttpClientError:目标服务器未响应

21

我正在尝试使用HTTP客户端通过PoolingClientConnectionManager命中服务器,为各个主机设置最大连接数。

//Code that initializes my connection manager and HTTP client 
HttpParams httpParam = httpclient.getParams();
HttpConnectionParams.setSoTimeout(httpParam, SOCKET_TIMEOUT);

HttpConnectionParams.setConnectionTimeout(httpParam, CONN_TIMEOUT);

httpclient.setParams(httpParam);

//Run a thread which closes Expired connections
new ConnectionManager(connManager).start(); 

//Code that executes my request 
HttpPost httpPost = new HttpPost(url);
        HttpEntity httpEntity = new StringEntity(request, "UTF-8");
httpPost.setEntity(httpEntity);

Header acceptEncoding = new BasicHeader("Accept-Encoding", "gzip,deflate");
httpPost.setHeader(acceptEncoding);     

if(contenttype != null && !contenttype.equals("")){
    Header contentType = new BasicHeader("Content-Type", contenttype);
    httpPost.setHeader(contentType);
}
InputStream inputStream = null;
LOG.info(dataSource + URL + url + REQUEST + request);

HttpResponse response = httpclient.execute(httpPost);

也就是说,我们正在使用连接池来实现HTTP持久化。

我们偶尔会遇到这个错误:

The target server failed to respond
org.apache.http.NoHttpResponseException: The target server failed to respond
        at org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:95)
        at org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:62)
        at org.apache.http.impl.io.AbstractMessageParser.parse(AbstractMessageParser.java:254)
        at org.apache.http.impl.AbstractHttpClientConnection.receiveResponseHeader(AbstractHttpClientConnection.java:289)
        at org.apache.http.impl.conn.DefaultClientConnection.receiveResponseHeader(DefaultClientConnection.java:252)
        at org.apache.http.impl.conn.ManagedClientConnectionImpl.receiveResponseHeader(ManagedClientConnectionImpl.java:191)
        at org.apache.http.protocol.HttpRequestExecutor.doReceiveResponse(HttpRequestExecutor.java:300)
        at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:127)
        at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:517)
        at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:906)

有人知道如何解决这个问题吗?

我们也正在关闭空闲连接。

请帮忙解决,谢谢。

http://hc.apache.org/httpcomponents-client-ga/tutorial/html/connmgmt.html


请发布您的客户端初始化和请求执行代码。 - vzamanillo
//用于初始化连接管理器和HTTP客户端的代码 HttpParams httpParam = httpclient.getParams(); HttpConnectionParams.setSoTimeout(httpParam, SOCKET_TIMEOUT); HttpConnectionParams.setConnectionTimeout(httpParam, CONN_TIMEOUT); httpclient.setParams(httpParam); //启动一个线程来关闭已过期的连接 new ConnectionManager(connManager).start(); - Balaji V
//执行我的请求的代码 HttpPost httpPost = new HttpPost(url); HttpEntity httpEntity = new StringEntity(request, "UTF-8"); httpPost.setEntity(httpEntity); Header acceptEncoding = new BasicHeader("Accept-Encoding", "gzip,deflate"); httpPost.setHeader(acceptEncoding); if(contenttype != null && !contenttype.equals("")){ Header contentType = new BasicHeader("Content-Type", contenttype); httpPost.setHeader(contentType); } InputStream inputStream = null; HttpResponse response = httpclient.execute(httpPost); - Balaji V
嗨,我已经粘贴了代码片段,请帮助我,谢谢。 - Balaji V
看起来这个 bug 仍然存在!你解决了这个问题吗? - Arya
2个回答

18

7
我正在使用4.4.1版本,不幸的是,我遇到了相同的问题。 - arjuncc

9

最近在使用 HttpClient 5 时遇到了类似的问题。

开启 HttpClient 日志后发现,问题是由于连接过期导致的。

添加以下内容有助于解决此问题,它可以检测和验证已经变得陈旧且处于空闲池中的连接,在重用之前。

PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();

connectionManager.setValidateAfterInactivity(timeinmilliseconds);

6
你用了什么数值作为 timeinmilliseconds - alexanoid

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接