I am using HTTPURLConnection
to connect to a web service. I know how to use HTTPURLConnection
but I want to understand how it works. Basically, I want to know the following:
- On which point does
HTTPURLConnection
try to establish a connection to the given URL? - On which point can I know that I was able to successfully establish a connection?
- Are establishing a connection and sending the actual request done in one step/method call? What method is it?
- Can you explain the function of
getOutputStream
andgetInputStream
in layman's term? I notice that when the server I'm trying to connect to is down, I get anException
atgetOutputStream
. 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
? - Am I correct to say that
openConnection
simply creates a new connection object but does not establish any connection yet? - How can I measure the read overhead and connect overhead?
Tim Bray presented a concise step-by-step, stating that openConnection() does not establish an actual connection. Rather, an actual HTTP connection is not established until you call methods such as getInputStream() or getOutputStream().
http://www.tbray.org/ongoing/When/201x/2012/01/17/HttpURLConnection
On which point does HTTPURLConnection try to establish a connection to the given URL?
It's worth clarifying, there's the 'UrlConnection' instance and then there's the underlying Tcp/Ip/SSL socket connection, 2 different concepts. The 'UrlConnection' or 'HttpUrlConnection' instance is synonymous with a single HTTP page request, and is created when you call url.openConnection(). But if you do multiple url.openConnection()'s from the one 'url' instance then if you're lucky, they'll reuse the same Tcp/Ip socket and SSL handshaking stuff...which is good if you're doing lots of page requests to the same server, especially good if you're using SSL where the overhead of establishing the socket is very high.
See: HttpURLConnection implementation
The first 3 answers to your questions are listed as inline comments, beside each method, in the example HTTP POST above.
From getOutputStream:
Basically, I think you have a good understanding of how this works, so let me just reiterate in layman's terms.
getOutputStream
basically opens aconnectionstream, with the intention of writing data to the server. In the above code example "message" could be a comment that we're sending to the server that represents a comment left on a post. When you seegetOutputStream
, you're opening theconnectionstream for writing, but you don't actually write any data until you callwriter.write("message=" + message);
.From getInputStream():
getInputStream
does the opposite. LikegetOutputStream
, it also opens aconnectionstream, but the intent is to read data from the server, not write to it. If the connection or stream-opening fails, you'll see aSocketTimeoutException
.Keep in mind that sending a request and sending data are two different operations. When you invoke
getOutputStream or getInputStreamurl.openConnection()
, you send a request to the server to establish a connection. There is a handshake that occurs where the server sends back an acknowledgement to you that the connection is established. It is then at that point in time that you're prepared to send or receive data. Thus, you do not need to call getOutputStream toestablish a connectionopen a stream, unless your purpose for making the request is to send data.In layman's terms, making a
getInputStream
request is the equivalent of making a phone call to your friend's house to say "Hey, is it okay if I come over and borrow that pair of vice grips?" and your friend establishes the handshake by saying, "Sure! Come and get it". Then, at that point, the connection is made, you walk to your friend's house, knock on the door, request the vice grips, and walk back to your house.Using a similar example for
getOutputStream
would involve calling your friend and saying "Hey, I have that money I owe you, can I send it to you"? Your friend, needing money and sick inside that you kept it for so long, says "Sure, come on over you cheap bastard". So you walk to your friend's house and "POST" the money to him. He then kicks you out and you walk back to your house.Now, continuing with the layman's example, let's look at some Exceptions. If you called your friend and he wasn't home, that could be a 500 error. If you called and got a disconnected number message because your friend is tired of you borrowing money all the time, that's a 404 page not found. If your phone is dead because you didn't pay the bill, that could be an IOException. (NOTE: This section may not be 100% correct. It's intended to give you a general idea of what's happening in layman's terms.)
Question #5:
Yes, you are correct that openConnection simply creates a new connection object but does not establish it. The connection is established when you call either getInputStream or getOutputStream.openConnection
creates a new connection object. From the URL.openConnection javadocs:The connection is established when you call openConnection, and the InputStream, OutputStream, or both, are called when you instantiate them.
Question #6:
To measure the overhead, I generally wrap some very simple timing code around the entire connection block, like so:
I'm sure there are more advanced methods for measuring the request time and overhead, but this generally is sufficient for my needs.
For information on closing connections, which you didn't ask about, see In Java when does a URL connection close?.
On the port named in the URL if any, otherwise 80 for HTTP and 443 for HTTPS. I believe this is documented.
When you call getInputStream() or getOutputStream() or getResponseCode() without getting an exception.
No and none.
Either of them first connects if necessary, then returns the required stream.
See above.
Yes.
Connect: take the time getInoutStream() or getOutputStream() takes to return, whichever you call first. Read: time from starting first read to getting the EOS.