[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

URLConnection and HttpURLConnection

Posted on 2005-05-10
6
Medium Priority
?
2,982 Views
Last Modified: 2008-03-17
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
Comment
Question by:cofactor
  • 2
  • 2
  • 2
6 Comments
 
LVL 92

Expert Comment

by:objects
ID: 13975291
use HttpURLConnection when you need to access http specific methods, otherwise use URLConnection.
Typically you can always use URLConnection.
0
 

Author Comment

by:cofactor
ID: 13975323
>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
 
LVL 92

Accepted Solution

by:
objects earned 120 total points
ID: 13975333
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:cofactor
ID: 13975353
thanks objects.  glad to get answer from the top expert !

here are the points.
0
 
LVL 16

Expert Comment

by:gnoon
ID: 13975493
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
 
LVL 16

Expert Comment

by:gnoon
ID: 13975505
Oops! already accepted on my typing ;-)
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This video teaches viewers about errors in exception handling.
Suggested Courses

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question