In order to send a string data to the server once, I do as below:
Make “HttpURLConnection” to my URL address and open it
Set the required headers
for the connection I Set setDoOutput to True
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.

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:
Method, Path, ProtocolKey-PairsDataRunning
my_output.write()will just add bytes to the body untilmy_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).