0

In a Java, I want to send HttpPost every 5 secs without waiting for the response. How can I do that?

I use the following code:

HttpClient httpClient = new DefaultHttpClient();
HttpPost post = new HttpPost(url);
StringEntity params = new StringEntity(json.toString() + "\n");
post.addHeader("content-type", "application/json");
post.setEntity(params);
httpClient.execute(post);

Thread.sleep(5000);

httpClient.execute(post);

but it does not work.

Even though I lose the previous connection and set up a new connection to send the second, the second execute function is always blocked.

user2552010
  • 23
  • 2
  • 6
  • Is this code in a loop? – fvrghl Jul 05 '13 at 00:57
  • It does not work is not a diagnosis. What exactly do you mean by "blocked" and "does not work"? – kolossus Jul 05 '13 at 01:00
  • "blocked" means "httpClient.execute(post)" will always wait for the data (or response), if no, it will be always blocked, it doesn't work means -- If I run my program like this, I will get the exception : java.lang.IllegalStateException: Invalid use of BasicClientConnManager: connection still allocated. Make sure to release the connection before allocating another one. – user2552010 Jul 05 '13 at 05:35

3 Answers3

3

Your question leaves a bunch of questions, but the basic point of it can be achieved by:

while(true){ //process executes infinitely. Replace with your own condition

  Thread.sleep(5000); // wait five seconds
  httpClient.execute(post); //execute your request

}
kolossus
  • 20,559
  • 3
  • 52
  • 104
  • I think I have to release the previous connection before sending a new request. But I don't want to use "EntityUtils.consume(entity);" because if there is no response from the server, it will be always blocked in "httpClient.execute(post)". – user2552010 Jul 05 '13 at 04:22
1

I tried your code and I got the exception : java.lang.IllegalStateException: Invalid use of BasicClientConnManager: connection still allocated. Make sure to release the connection before allocating another one.

This exception is already logged in HttpClient 4.0.1 - how to release connection?

I was able to release the connection by consuming the response with the following code:

public void sendMultipleRequests() throws ClientProtocolException, IOException, InterruptedException {
    HttpClient httpClient = new DefaultHttpClient();
    HttpPost post = new HttpPost("http://www.google.com");
    HttpResponse response = httpClient.execute(post);

    HttpEntity entity = response.getEntity();
    EntityUtils.consume(entity);

    Thread.sleep(5000);

    response = httpClient.execute(post);
    entity = response.getEntity();
    EntityUtils.consume(entity);
}
Community
  • 1
  • 1
yannic
  • 11
  • 2
  • Based on your reply, if the server doesn't send any response to my HttpClient, the code “response = httpClient.execute(post);” will always blocked, right? If I don't want to be blocked, what should I do? – user2552010 Jul 05 '13 at 04:16
  • Is there any way to keep posting the request every 5 secs and ignore the response (maybe there is no response sending back to me from the server)? – user2552010 Jul 05 '13 at 04:25
1

Using DefaultHttpClient is synchronous which means that program is blocked waiting for the response. Instead of that you could use async-http-client library to perform asynchronous requests (you can download jar files from search.maven.org if you're not familiar with Maven). Sample code may look like:

import com.ning.http.client.*; //imports

try {
        AsyncHttpClient asyncHttpClient = new AsyncHttpClient();

        while(true) {

            asyncHttpClient
                    .preparePost("http://your.url/")
                    .addParameter("postVariableName", "postVariableValue")
                    .execute(); // just execute request and ignore response

            System.out.println("Request sent");

            Thread.sleep(5000);
        }
    } catch (Exception e) {
        System.out.println("oops..." + e);
    }
Paweł Piecyk
  • 2,749
  • 15
  • 17
  • I try your code but still get blocked for the next request if no response. – user2552010 Jul 05 '13 at 16:39
  • Could you post the code you've tested? Is it exactly the same code as mine? I've tested it before posting and everything works well. How do you concluded that execution of program is blocked? – Paweł Piecyk Jul 05 '13 at 22:45