Hello everyone,


I have found that class "URLConnection" has methods to establish a connection, for example, method "openConnection" and method "connect". But I have not found any methods of this class which are dealing with disconnect. I am wondering if I use class "URLConnection" to establish an HTTP connection to a remote host, what is the correct, clean and efficient approach to disconnect from remote host or close the connection.


Thanks in advance,
George

Recommended Answers

All 6 Replies

1) for HTTP why not use HttpURLConnection instead?
2) HTTP is a stateless protocol meaning the server will close the connection for you.
3) if you close all streams you get from the URLConnection the connection is gone.

Thanks jwenting,

1) for HTTP why not use HttpURLConnection instead?

The object (reference) type which I can get is URLConnection other than HttpURLConnection, I think I have no means to invoke disconnect method. Am I correct?

2) HTTP is a stateless protocol meaning the server will close the connection for you.
3) if you close all streams you get from the URLConnection the connection is gone.

Your 2 comments are very helpful. Even though the server will close the connection for me, I am wondering how to release the resources on the client side. Just close all the streams associated with the connection?


regards,
George

you can test whether you're getting an HttpURLConnection (you well may) using the instanceof operator.
Would open up a lot of nice extra functionality dedicated to http processing.

Yes, always close connections. If it's already closed there may be an exception thrown, the documentation will help (or else a good book on network programming, I like O'Reilly's book).
That's not just true for this class, you should always clean up after yourself no matter what you're using (so Connections, Sockets, Streams, etc.).

Thanks jwenting,

you can test whether you're getting an HttpURLConnection (you well may) using the instanceof operator.
Would open up a lot of nice extra functionality dedicated to http processing.

Yes, always close connections. If it's already closed there may be an exception thrown, the documentation will help (or else a good book on network programming, I like O'Reilly's book).
That's not just true for this class, you should always clean up after yourself no matter what you're using (so Connections, Sockets, Streams, etc.).

Your comments are very helpful. Do you mean I should use the following approach to ensure all the resources are released in a clean way?

- Cast to HttpURLConnection;
- Using HttpURLConnection;
- Close all the streams associated with HttpURLConnection handle;
- Invoke disconnect method of HttpURLConnection.

Am I correct? If I made something wrong or if I have missed anything, please let me know.


regards,
George

do the cast only if it really is an HttpURLConnection of course, but basically yes.

Thanks jwenting,

do the cast only if it really is an HttpURLConnection of course, but basically yes.

All your help on this thread are very helpful.


regards,
George

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.