Home > Mobile >  can I send same HTTP request with same HttpURLConnection multiple times?
can I send same HTTP request with same HttpURLConnection multiple times?

Time:09-02

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.

CodePudding user response:

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).

CodePudding user response:

thanks to paulsm4 and null who gave me a better insight toward http connection persistence and life cycle!! but about the second part of my question, any other alternative to send the request to the server with the same 3 main parts? since making new connections and setting headers take times which decrease my performance at this part of code, I want to send them as fast as possible. would using asynchronous http request helpful at this predicament? Thanks all.

CodePudding user response:

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).

  • Related