HttpClientError:目标服务器无法响应

时间:2015-03-17 16:32:38

标签: java apache httpclient connection-pooling

我正在尝试使用http客户端使用PoolingClientConnectionManager设置单个主机的最大连接数

来命中服务器
 //Code that inilizes my connection mananger 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

2 个答案:

答案 0 :(得分:12)

可能是HttpClient中的一个错误。

如果您使用的是HttpClient 4.4,请尝试升级到4.4.1。

如果您想了解更多信息,请查看this link

如果您无法升级,以下链接可能会有所帮助。

http://www.nuxeo.com/blog/using-httpclient-properly-avoid-closewait-tcp-connections/

祝你好运!

答案 1 :(得分:3)

最近在使用HttpClient 5时面临类似的情况。

启用HttpClient日志并发现问题是由于陈旧连接造成的。

添加以下内容有助于解决问题,它会在重用之前检测并验证已变为陈旧且在池中保持不活动状态的连接。

PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();

connectionManager.setValidateAfterInactivity(timeinmilliseconds);