Why does my client only submit two requests?

Asked

Viewed 30 times

1

My Httpclient is only sending two requests. The server receives, returns the 2 values but it is actually a loop that sends a lot of times! Can someone help me?

public static void main(String[] args) throws IOException, InterruptedException {
    SendData sender = new SendData();
    String JsonDeDados;
    String infos = "informacoes";

    HttpClient client = HttpClientBuilder.create().build();
    CloseableHttpClient clientclose = HttpClients.createDefault();
    HttpPost post = new HttpPost("http://localhost:5030/data");

    do {

        JsonDeDados = sender.sendMessageDATA();
        List<NameValuePair> urlParameters = new ArrayList<NameValuePair>();
        urlParameters.add(new BasicNameValuePair(infos, JsonDeDados));
        StringEntity entity = new StringEntity(JsonDeDados);

        post.setEntity(entity);
        post.setHeader("Content-Type", "application/json");

        CloseableHttpResponse response = (CloseableHttpResponse) client.execute(post);
        System.out.println("Código da Resposta: " + response.getStatusLine().getStatusCode());
        clientclose.close();
        Thread.sleep(1000);

    }while(true);

}
  • How is the loop structure done? Without the loop, it is difficult to guess what is happening in the execution flow control

  • is one of the normal while(true), to send eternally

  • I edited the code to be more complete, but I’ve already been downvoted :c

  • clientClose is instantiated once and closed several times? And is not used once if you want before being closed?

1 answer

1


I think I might be missing the remote .releaseConnection(); on your Httppost. Possibly will stay:

post.releaseConnection();

Try that, if it’s not, it might be something in your loop

  • Where can I position this line? After the clientclose.close(); ?

  • Yeah, that’s right. Try and tell me if it worked.

  • It worked, you could give a quick explanation about what this command does?

  • Then, as the command itself says, it "drops" the connection. So you don’t get a "queue" of requests. As it is inside the loop, it opens the connection, sends, closes. Opens, sends, closes. And so it goes. I hope to have helped, and I recommend to read this article

Browser other questions tagged

You are not signed in. Login or sign up in order to post.