On which point does
HTTPURLConnection
try to establish a connection to the given URL?
On the port named in the URL if any, otherwise 80 for HTTP and 443 for HTTPS. I believe this is documented.
On which point can I know that I was able to successfully establish a connection?
When you call getInputStream()
or getOutputStream()
or getResponseCode()
without getting an exception.
Are establishing a connection and sending the actual request done in one step/method call? What method is it?
No and none.
Can you explain the function of
getOutputStream()
andgetInputStream()
in layman's term?
Either of them first connects if necessary, then returns the required stream.
I notice that when the server I'm trying to connect to is down, I get an Exception at
getOutputStream()
. Does it mean thatHTTPURLConnection
will only start to establish a connection when I invokegetOutputStream()
? How about thegetInputStream()
? Since I'm only able to get the response atgetInputStream()
, then does it mean that I didn't send any request atgetOutputStream()
yet but simply establishes a connection? DoHttpURLConnection
go back to the server to request for response when I invokegetInputStream()
?
See above.
Am I correct to say that
openConnection()
simply creates a new connection object but does not establish any connection yet?
Yes.
How can I measure the read overhead and connect overhead?
Connect: take the time getInputStream()
or getOutputStream()
takes to return, whichever you call first. Read: time from starting first read to getting the EOS.