I have been using various HTTP clients in java for getting various pieces of information from websites or for using web services (like the google maps geocoder)
The current client I'm working with is the HTTP Components client from Apache.
What I always wonder is why there is no .getText() or .getBytes() method that simply gives me all the contents of the url, but instead there are methods that return InputStream. What is the point or the reason for that?
It's not like I continuously comunicate with the server so that I would have to keep checking the stream. HTTP is stateless, so why is there no getText()/getBytes() method that simply returns the contents of the HTTP response (as String or byte[]), after it was received?
Because the server starts outputting the result in a stream, and the client gets it that way. You can buffer that stream in memory and transform it to a String
/ byte[]
, but you can also process parts of it, and discard them.
Because it's a network; because it's a stream; because the server response can be arbitrarily large. Same reasoning applies to files.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With