Many examples I've seen don't explicitly call connect()
. Instead they just use getInputStream()
or getResponseCode()
.
I'm assuming all of these HttpURLConnection methods that require a connection just call connect()
themselves?
Are there any cases where connect()
must be explicitly called for an HttpURLConnection?
No, there are no cases. It's implicitly executed on demand. It's even specified in the documentation. Here's an extract of the URLConnection#connect()
javadoc:
Operations that depend on being connected, like getContentLength
, will implicitly perform the connection, if necessary.
connect()
is implied and automatically invoked when needed.