1

In order to send a string data to the server once, I do as below:

  1. Make “HttpURLConnection” to my URL address and open it

  2. Set the required headers

  3. for the connection I Set setDoOutput to True

  4. new a DataOutputStream from my connection and finally write my string data to it.

    HttpURLConnection  myConn = (HttpURLConnection);
    myUrl.openConnection();
    myConn.setRequestProperty("Accept", "application/json, text/plain, */*");
    myConn.setDoOutput(true);
    DataOutputStream my_output = new DataOutputStream(myConn.getOutputStream());
    my_output.write(myData.getBytes("UTF-8"));
    

But what to do if I want to send exactly the same data with same URl and headers multiple times? Can I write to it multiple times?(I mean that is it possible to use the last line of code multiple times?) Or should I repeat the above steps and try it with a new connection? And if yes should I wait for some second or millisecond before sending the next one? I also searched for some other alternatives such as “HttpClient” Http API and making synchronous Http request which as far as I got can help me setting the headers only once. At the end, I appreciate your help and support and any other alternatives would be welcome. Thanks a million.

paulsm4
  • 114,292
  • 17
  • 138
  • 190
  • 1
    Q: what to do if I want to send exactly the same data with same URl and headers multiple times? A: You can write "myData" as many times as you want. It will simply duplicate the text ... in the same connection. If you want to repeat the same combination of HTTP header and body ... then you'll need to open a new connection. – paulsm4 Sep 01 '22 at 17:42

2 Answers2

0

I understand that the question has be answered in the comments, but I am leaving this here so that future viewers can see it.

An HTTP request contains 3 main parts:

  • Request Line: Method, Path, Protocol
  • Headers: Key-Pairs
  • Body: Data

Running my_output.write() will just add bytes to the body until my_output.flush() has been executed. Flushing the stream will write the data to the server.

Because HTTP requests are usually closed by the server once all data has been sent/received, whether or not you create a new connection or just add on to the body depends on your intentions. Typically, clients will create a new connection for each request because each response should be handled individually, rather than sending a repetitive body. This will vary though because some servers choose to hold a connection (such as WebSockets).

null
  • 66
  • 9
0

If you are open to external libraries, you may find this chart insightful: HTTP Library Chart

AsyncHttpClient would be a good fit for your intentions.

Alternatively, you can use cURL by running a terminal command with Runtime.getRuntime().exec(). More information about using cURL with POST Requests can be found here. While cURL is efficient, you have to depend on the fact that your OS supports the command (though usually all devices that can run Java have this command).

null
  • 66
  • 9