• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3406
  • Last Modified:

URLConnection and HttpURLConnection

we can use URLConnection and HttpURLConnection  both for connectiong a url and getting streams data etc.

i just dont understand when should i use URLConnection  and when when should i use HttpURLConnection.

Tell me a situation to visualize the difference.
thanks
0
cofactor
Asked:
cofactor
  • 2
  • 2
  • 2
1 Solution
 
objectsCommented:
use HttpURLConnection when you need to access http specific methods, otherwise use URLConnection.
Typically you can always use URLConnection.
0
 
cofactorAuthor Commented:
>use HttpURLConnection when you need to access http specific methods, otherwise use URLConnection.
i guessed the answer would be like this...bcoz Name of  the class itself says that.

so, u mena to say, URLConnection  covers everything but  HttpURLConnection covers only http protocol.


>when you need to access http specific methods.

can u plz tell  what are non http methods ? u mean when i would use Datagrams ? right ?
so, Still in this  case i can use URLConnection but Not HttpURLConnection...is that something u wanted to say ultimately ?


can u plz explain a bit about the difference ?
thanks for the quick answer
0
 
objectsCommented:
HttpURLConnection is a subclass of URLConnection containing http specific methods.
When you open a connection the actual URLConnection subclass that is returned depends on what protocol the URL uses.
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
cofactorAuthor Commented:
thanks objects.  glad to get answer from the top expert !

here are the points.
0
 
gnoonCommented:
You can use them like this

// when you connect to HTTP site
String site = "http://www.verisign.com";
URL url = new URL(site);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();

or

// when you connect to HTTPS site (secure site)
String site = "https://www.verisign.com";
URL url = new URL(site);
HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();

but almost use this for safety

// when you connect to the site in a
String site = "http://www.verisign.com";
URL url = new URL(site);
URLConnection conn =  url.openConnection();
if(conn instanceof HttpURLConnection)
{
    // process as HTTP here
    HttpURLConnection http = (HttpURLConnection) conn;
}
else if(conn instanceof HttpsURLConnection)
{
    // process as HTTPS here
    HttpsURLConnection https = (HttpsURLConnection) conn;
}
else
{
    // process as general connection here
}
0
 
gnoonCommented:
Oops! already accepted on my typing ;-)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 2
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now