A class called java.net.URLConnection provides another approach to URLs.
-
URLConnection provides the most general type of HTTP connectivity and indeed has some advantages over sockets (see following foils) as these are subject to special security restrictions in some browsers.
|
A method URL.openConnection() returns an instance of class URLConnection:
-
URLConnection conn = url.openConnection();
-
conn.connect(); // open a connection
-
Now use this URLConnection object to open a stream:
-
BufferedReader in =
-
new BufferedReader(
-
new InputStreamReader( conn.getInputStream() ) );
|
Note that one can connect not just to HTML files but also to CGI scripts and other web documents.
|