Apache HTTPClient 4.3.3执行GET请求块的方法,永不返回

时间:2014-02-27 13:21:23

标签: java apache rest apache-httpclient-4.x apache-httpcomponents

从早上开始面对这个奇怪的问题,我正在对一个特定的端点进行REST调用,该端点在页面中给出响应,因此我需要一次又一次地进行调用,直到所有页面都完成为止。我的代码工作正常,花花公子直到最后一页之后的最后一页我的下一个请求(应该返回一个空白页面)httpClient.execute(httpGet);被永久阻止,永远不会回来或者都不会抛出任何异常。如果我设置了连接请求超时,那么最后一次调用没有被阻止,并且无法说超时但是我没有理解为什么最后一次调用不能正常工作如果我从firefox RESTClient进行相同的最后一次调用它可以工作。请帮助。

我还尝试添加不同的日志级别,如System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.SimpleLog");和其他日志来调试,但没有运气。

以下是我的代码。

private static Servers fetchServers(String token,String endpoint) throws Exception{
    JsonReader jreader = null;
    InputStreamReader isr = null;
    CloseableHttpClient httpClient = null;
    try{
        URI uri = new URI(endpoint);

        /** accepting all certificates */
        httpClient = getSecuredHttpClient();

        uri = new URIBuilder(uri)
                .setParameter("limit", "1")//Page limit, for testing have kept just 1 since i have only 2 records.
                .build();

        HttpGet httpGet = new HttpGet(uri);

        httpGet.setHeader(HTTP.CONTENT_TYPE, "application/json");
        httpGet.setHeader("X-Auth-Token", token);
        httpGet.setHeader("accept", "application/json");
        httpGet.setHeader(HTTP.USER_AGENT, "python-neutronclient");

        Servers cloudServers = new Servers();
        cloudServers.setServers(new ArrayList<Server>());

        boolean nextPage = false;

        do {
                HttpResponse resp = httpClient.execute(httpGet);//this is where it gets blocked for last page.
                if(resp.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
                    isr = new InputStreamReader(resp.getEntity().getContent());
                    jreader = new JsonReader(isr);
                    Gson gson = new GsonBuilder().registerTypeAdapter(Address.class, new AddressAdapter()).create();

                    Servers servers = gson.fromJson(jreader, Servers.class);
                    cloudServers.getServers().addAll(servers.getServers());

                    if(servers.getServersLinks() == null || servers.getServersLinks().size()==0)
                        nextPage = false;
                    else if(servers.getServersLinks().get(0).getRel().equals("next"))
                        nextPage = true;

                    uri = new URI(servers.getServersLinks().get(0).getHref());//this gives the url for next page.
                    httpGet.setURI(uri);
                }else{
                    break;
                }
        } while (nextPage);
        return cloudServers;
    }catch(Exception e){
        throw e;
    }finally{
        if(null != jreader){
            jreader.close();
        }
        if(null != isr){
            isr.close();
        }
        if(null != httpClient){
            httpClient.close();
        }
    }
}

非常感谢任何帮助。

-Regards 将

1 个答案:

答案 0 :(得分:14)

我很确定您的代码只是泄漏并最终耗尽连接。

do {
    CloseableHttpResponse resp = httpClient.execute(httpGet);
    try {

    // Do what you have to do 
    // but make sure the response gets closed no matter what
    // even if do not care about its content

    } finally {
        resp.close();
    }        
} while (nextPage);