wood burning stoves 2.0*
The moose likes Sockets and Internet Protocols and the fly likes Java URLConnection - When do I need to use the connect() method? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "Java URLConnection - When do I need to use the connect() method?" Watch "Java URLConnection - When do I need to use the connect() method?" New topic
Author

Java URLConnection - When do I need to use the connect() method?

Karim Kiswarday
Ranch Hand

Joined: Oct 01, 2010
Posts: 50

I have a problem to understand the meaning of the connect() method in the URLConnection class.
In the following code, if I use the connect() method, I get the same result if I don't use it.
Why (or when) do I need to use it?



No fun at the table, no fun in the sack.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18991
    
    8

Well, the documentation for that method says

Operations that depend on being connected, like getContentLength, will implicitly perform the connection, if necessary.


In other words, if you don't call connect() but you call some other method which needs to be connected, that other method will call connect() itself if it isn't already connected. Does that clarify things for you?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java URLConnection - When do I need to use the connect() method?