Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Karim Kiswarday
Ranch Hand
Posts: 50
Eclipse IDE Java PHP
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?

 
Paul Clapham
Sheriff
Pie
Posts: 20203
26
MySQL Database
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic