aspose file tools
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


Win a copy of Groovy Fundamentals video training course this week in the Groovy forum!
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
Sheriff

Joined: Oct 14, 2005
Posts: 19553
    
  10

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?
 
wood burning stoves
 
subject: Java URLConnection - When do I need to use the connect() method?