简体   繁体   中英

can I send same HTTP request with same HttpURLConnection multiple times?

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.

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

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.

If you are open to external libraries, you may find this chart insightful: HTTP 库图表

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM