1 |
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.
|
2 |
A method URL.openConnection() returns an instance of class URLConnection:
|
3 |
URLConnection conn = url.openConnection();
|
4 |
conn.connect(); // open a connection
|
5 |
Now use this URLConnection object to open a stream:
|
6 |
BufferedReader in =
-
new BufferedReader(
-
new InputStreamReader( conn.getInputStream() ) );
|
7 |
Note that one can connect not just to HTML files but also to CGI scripts and other web documents.
|