What is the function or purpose of HttpURLConnection.setDoInput
and HttpURLConnection.setDoOutput
?
setDoInput(false) is called when the response body is going to be ignored. The Java core code does call this for JAXB and XML serialization to URLs.
setDoOutput(true) is used for POST and PUT requests. If it is false then it is for using GET requests. Follow this answer to receive notifications.
URLConnection is an abstract class whose subclasses form the link between the user application and any resource on the web. We can use it to read/write from/to any resource referenced by a URL object. There are mainly two subclasses that extend the URLConnection class.
From the documentation for URLConnection
:
Set the DoInput flag to true if you intend to use the URL connection for input, false if not. The default is true.
Set the DoOutput flag to true if you intend to use the URL connection for output, false if not. The default is false.
As for the actual purpose, it doesn't appear that anything in the Oracle code checks on the doInput
variable. However the doOutput
default of false will prevent you from calling getOutputStream()
necessary for HTTP POST requests. So it is a way to indicate ahead of time that you expect to write to an OutputStream
without actually establishing it.
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