Ryoichiro Oka
Ryoichiro Oka

Reputation: 1997

What happens by calling HttpURLConnection.getInputStream() more than twice

Does the HttpURLConnection.getInputStream() method fetch the contents with connecting to the server each time, or save the contents in the memory first time and give it back again from the second time? I'm building a function that takes in an HttpURLConnection instance and judges if the contents satisfy some criteria to be processed by another function later. Thank you!

Upvotes: 1

Views: 559

Answers (1)

Sotirios Delimanolis
Sotirios Delimanolis

Reputation: 280000

The actual answer is in HttpURLConnection#connect() which states

If the connect method is called when the connection has already been opened (indicated by the connected field having the value true), the call is ignored.

If you haven't called connect(), getInputStream() will call it itself. If you have, it will just be ignored.

You can find the source code here.

Upvotes: 4

Related Questions