httpclient无法发送两个以上的请求

sha*_*kha 3 apache tomcat6 apache-httpclient-4.x

我正在编写一个客户端服务器应用程序。我CloseableHttpClient在客户端上用于发送GET请求。我想向服务器发送5个GET请求。但是,只有我对前两个请求的响应。这是我的客户代码:

 import java.io.IOException;

import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;


public class Test {

    public static void main(String[] args) {
        HttpGet getreq = new HttpGet("http://shalakha:8089/Gateway/ReverseInvokeListener");
        CloseableHttpClient c1 = HttpClients.createMinimal();
        try {
            for(int i=0;i<5;i++){
            CloseableHttpResponse resp = c1.execute(getreq);
            System.out.println(resp);
        }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
Run Code Online (Sandbox Code Playgroud)

这是我得到的答复:

HTTP/1.1 200 OK [Server: Apache-Coyote/1.1, Set-Cookie: JSESSIONID=0D4ABDC362FEC3030A5EE3709F3096FD; Path=/Gateway, CLIENTREQ: NO, Content-Length: 0, Date: Thu, 09 Apr 2015 09:22:22 GMT]
HTTP/1.1 200 OK [Server: Apache-Coyote/1.1, CLIENTREQ: NO, Content-Length: 0, Date: Thu, 09 Apr 2015 09:22:26 GMT]
Run Code Online (Sandbox Code Playgroud)

其中“ CLIENTREQ”​​是我在发送响应时从服务器端添加的CUSTOM标头。

关于什么原因的任何指示?的connectionTimeoutserver.xml被设定为“-1”,其表示无限超时。

ok2*_*k2c 7

您的代码泄漏了所有可用的连接(默认情况下为两个),并且耗尽了连接池。您需要关闭响应对象,以确保将连接释放回池中。

http://hc.apache.org/httpcomponents-client-4.4.x/tutorial/html/fundamentals.html#d5e145