This week's book giveaway is in the OCAJP 8 forum.
We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line!
See this thread for details.
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 OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 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: 19747
    
  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?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java URLConnection - When do I need to use the connect() method?
 
It's not a secret anymore!