img
System.out.println("Host: " + hp.getHost());
System.out.println("File: " + hp.getFile());
System.out.println("Ext:" + hp.toExternalForm());
}
}
When you run this, you will get the following output:
Protocol: http
Port: -1
Host: www.osborne
File: /downloads
Ext:http://www.osborne/downloads
Notice that the port is 1; this means that a port was not explicitly set. Given a URL object,
­
you can retrieve the data associated with it. To access the actual bits or content information of
a URL, create a URLConnection object from it, using its openConnection( ) method, like this:
urlc = url.openConnection()
openConnection( ) has the following general form:
URLConnection openConnection( ) throws IOException
It returns a URLConnection object associated with the invoking URL object. Notice that it
may throw an IOException.
URLConnection
URLConnection is a general-purpose class for accessing the attributes of a remote resource.
Once you make a connection to a remote server, you can use URLConnection to inspect the
properties of the remote object before actually transporting it locally. These attributes are
exposed by the HTTP protocol specification and, as such, only make sense for URL objects
that are using the HTTP protocol.
URLConnection defines several methods. Here is a sampling:
int getContentLength( )
Returns the size in bytes of the content associated
with the resource. If the length is unavailable, ­1 is
returned.
String getContentType( )
Returns the type of content found in the resource.
This is the value of the content-type header field.
Returns null if the content type is not available.
long getDate( )
Returns the time and date of the response
represented in terms of milliseconds since
Januar y 1, 1970 GMT.
long getExpiration( )
Returns the expiration time and date of the
resource represented in terms of milliseconds
since Januar y 1, 1970 GMT. Zero is returned if the
expiration date is unavailable.
Search WWH :
Custom Search
Previous Page
Java SE 6 Topic Index
Next Page
Java SE 6 Bookmarks
Home