I'm using the HttpURLConnection
to open connections to web pages. I call the connect()
method to open the connect开发者_StackOverflow社区ion.
I not found an isConnected()
method.
I need to know when the connection was established with the server. I need this because I tryinf to found a fast way to open multiple connections. One object open connections and another do some processing with the connections that established the communication with the server.
I want to let the processor always busy.
Not sure why you would need that information.
You should not care since once you call connect()
you can start retrieving the response e.g.
int responseCode = connection.getResponseCode();//Get HTTP status code
Assuming of course you have send all your request to the output stream.
The HttpURLConnection
under the hood knows if it is already connected to the server and the implementation reuses the connection (transparent to you).
Now, having said that to answer your question, I think that the only way to know if the HttpURLConnection
's state is connected to the server, is to call openConnection()
a second time in the same HttpURLConnection
object.
If it is already connected it will throw a relevant exception complaining that it is already connected something like: IllegalStateException("Already connected");
.
At that point you know that the HttpURLConnection
is connected
httpURLConnection con = new httpURLConnection(some URL object);
con.getHeaderField(httpURLConnection.HTTP_CREATED );
精彩评论