The URL and URLConnection classes encapsulate much of the complexity of retrieving information from a remote site. Here is how you specify a URL.
URL url = new URL(urlString);
The Java 2 platform supports both HTTP and FTP resources.
If you simply want to fetch the contents of the resource, then you can use the openStream method of the URL class. This method yields an InputStream object. Using this stream object, you can easily read the contents of the resource .
The java.net package makes a useful distinction between URL s
( uniform resource locators ) and URI s ( uniform resource identifiers ).
A URI is a purely syntactical construct that specifies the various parts of the string specifying a web resource. A URL is a special kind of URI , namely one with sufficient information to locate a resource. Other URIs, such as
are not locators—there is no data to locate from this identifier. Such a URI is called a URN (uniform resource name ).
After connecting to the server, you can query the header information. There are two methods, getHeaderFieldKey and getHeaderField , to enumerate all fields of the header. As of SDK 1.4, there is also a method getHeaderFields that gets a standard Map object containing the header fields. For your convenience, the following methods query standard fields.
Finally, you can access the resource data. Use the getInputStream method to obtain an input stream for reading the information. (This is the same input stream that the openStream method of the URL class returns.).
Some programmers form the wrong mental image when using the URLConnection class and think that the getInputStream and getOutputStream methods are similar to those of the Socket class. But that isn't quite true. The URLConnection class does quite a bit of magic behind the scenes, in particular the handling of request and response headers.
URL ( String url_str ) throws java.net.MalformedURLException—creates a URL object based on the string parameter. If the URL cannot be correctly parsed,a MalformedURLException will be thrown.
String getRef () — returns the reference component of a URL. A null value will be returned if no reference was specified.
URLConnection openConnection()— returns a URLConnection object, which can be used to establish a connection to the remote resource. The name of this method can be deceiving, though, as no connection will be established until further methods of the URLConnection object are invoked.
InputStream openStream () throws java.io.IOException — establishes a connection to the remote server where the resource is located, and provides an InputStream that can be used to read the resource's contents. This method provides a quick and easy way to retrieve the contents of a URL, without the added complexity of dealing with a URLConnection object.
There are two URL methods that can assist in retrieving the contents of a remote resource:
1. InputStream URL.openStream()
2. URLConnection URL.openConnection();
For greater control over how the request is made, a URLConnection object created by invoking the URL.openConnection () method would be used. In many situations, however, a simpler way to retrieve the contents of a resource is called for. The openStream () method returns an InputStream , which makes reading a resource simple.