0

I have written my client program in Java using apache HTTP client API. What I am trying to do is send a request to a server. If the response code of the request sent is not 200, Then I have to resend a request to the server. While doing so I get the following error.

    java.lang.IllegalStateException: Invalid use of BasicClientConnManager: connection still allocated.
Make sure to release the connection before allocating another one.
    at org.apache.http.impl.conn.BasicClientConnectionManager.getConnection(BasicClientConnectionManager.java:162)
    at org.apache.http.impl.conn.BasicClientConnectionManager$1.getConnection(BasicClientConnectionManager.java:139)
    at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:456)
    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:906)
    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:805)
    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:784)
    at main.ApacheGet.sendGet(ApacheGet.java:31)
    at main.ApacheGet.main(ApacheGet.java:17)

I am trying to achieve LongPolling. When I get a response, I send a new request. If I get response code other than 200, I send a new request. I do understand what this error means, but i dont know how to overcome it. Following is my code.

    private void sendGet() throws Exception {
            HttpClient client = new DefaultHttpClient();
            HttpGet request = new HttpGet(
                    "http://localhost:8080/MyServlet/Servlet?id=1001");
            System.out.println("Started");
            HttpResponse response;
            while (true) {
                System.out.print("looped back");
                response = client.execute(request);
                int status = response.getStatusLine().getStatusCode();
                System.out.println("Got the response. Status code:" + status);
                if (status == 200) {
                    StringBuffer textview = new StringBuffer();
                    // Get the response
                    BufferedReader rd = new BufferedReader(new InputStreamReader(
                            response.getEntity().getContent()));
                    String line = "";
                    while ((line = rd.readLine()) != null) {
                        textview.append(line);
                    }
                    System.out.print(textview.toString());
                }
                else{
                    continue;
                }
            }
     }

Thanks in advance :).

Ashwin Surana
  • 826
  • 3
  • 10
  • 33
  • you may want to put everything inside the `while` loop. That way you instantiate a new `HttpGet` instance every time and the error should not occure anymore – MatthiasLaug Jul 24 '13 at 05:53
  • also releasing the connection like mentioned in the exception might be a solution. Have look at `method.releaseConnection();` as documented at http://hc.apache.org/httpclient-3.x//tutorial.html – MatthiasLaug Jul 24 '13 at 05:54

0 Answers0