Java Reference
In-Depth Information
As you can see, the above code is very similar to the code from the last section. However,
an additional line follows the URL declaration. This line calls the openStream function,
and receives an InputStream object. You will see what to do with this object in the next
section.
The above code also has to deal with an additional exception. The IOException
can be thrown by the openStream function, so it is necessary to catch the exception.
Remember from the last section that the constructor of the URL class does not check to see
if a URL actually exists? This is where that is checked. If the URL does not exist, or if there
is any trouble connecting to the web server that holds that URL, then an IOException
will be thrown.
Now that you have constructed the URL object and opened a connection, you are ready
to download the data from that URL.
Downloading the Contents
Downloading the contents of a web page uses the same procedure you would use to
read data from any input stream. Just as if you were reading from a disk file, you will use the
read function of the stream. The following block of code reads the contents of the URL and
stores it into a StringBuilder .
try
{
URL u = new URL("http://www.httprecipes.com");
InputStream is = u.openInputStream();
StringBuilder result = new StringBuilder();
byte buffer[] = new byte[BUFFER_SIZE];
InputStream s = u.openStream();
int size = 0;
do
{
size = s.read(buffer);
if (size != -1)
result.append(new String(buffer, 0, size));
} while (size != -1);
System.out.println( result.toString() );
}
catch(MalformedURLException e)
{
System.out.println("Invalid URL");
Search WWH ::




Custom Search