pandu
pandu

Reputation: 21

Java HttpURLConnection class Program

I am learning Java through use of a textbook, which contains the following code describing the use of a HttpURLConnection ...

class HttpURLDemo {
    public static void main(String args[]) throws Exception {
        URL hp = new URL("http://www.google.com");
        HttpURLConnection hpCon = (HttpURLConnection) hp.openConnection();

        // Display request method.
        System.out.println("Request method is " + hpCon.getRequestMethod());
        }
    }

Could someone please explain why the hpCon object is declared in the following way...

HttpURLConnection hpCon = (HttpURLConnection) hp.openConnection();

instead of declaring it like this...

HttpURLConnection hpCon = new HttpURLConnection(); 

The textbook author provided the following explanation, which I don't really understand...

Java provides a subclass of URLConnection that provides support for HTTP connections. This class is called HttpURLConnection. You obtain an HttpURLConnection in the same way just shown, by calling openConnection( ) on a URL object, but you must cast the result to HttpURLConnection. (Of course, you must make sure that you are actually opening an HTTP connection.) Once you have obtained a reference to an HttpURLConnection object, you can use any of the methods inherited from URLConnection

Upvotes: 2

Views: 2543

Answers (2)

Cratylus
Cratylus

Reputation: 54074

java.net.URLConnection is an abstract class that facilitates in communication with various types of servers via various protocols (ftp http etc).

The protocol specific subclasses are hidden inside SUN's packages and these hidden classes are responsible for the concrete implementation of the protocols.

In your example since your URL is a http://www.google.com by parsing the URL the internals of the URL class knows that an HTTP handler/subclass must be used.
So when you open a connection to the server hp.openConnection(); you get a concrete instance of a class that implements the HTTP protocol.

That class is an instance of HttpURLConnection (actually a subclass since HTTPURLConnection is also abstract and that is why you can do:

HttpURLConnection hpCon = (HttpURLConnection) hp.openConnection(); and not get class cast exception.

So with Java's design you can't do HttpURLConnection hpCon = new HttpURLConnection(hp); as you ask, since that is not how the designers want you to use these APIs.

You are expected to work arround URLs and URLConnections and only worry about input/output.
You shouldn't worry about the rest

Upvotes: 1

Vitaly Olegovitch
Vitaly Olegovitch

Reputation: 3547

The declaration that you don't understand why not to use:

 HttpURLConnection hpCon = new HttpURLConnection();

Does not provide information about the URL to which you want to open the connection. This is the reason why you should use:

HttpURLConnection hpCon = new HttpURLConnection(hp);

Because this way the constructor knows that you want to open a connection to the url "http://www.google.com".

Upvotes: 4

Related Questions