Downloading the Web Page | Heaton Research

Downloading the Web Page

The downloadPage function accepts a URL, and downloads the contents of that URL into a String. The resulting String can then be parsed and data extracted from it.

This function begins by creating two buffers. The first buffer, named result, will hold the contents of the page, as it is downloaded. The second buffer, named buffer, will be used with the "read" function to read blocks of data from the URL.

StringBuffer result = new StringBuffer();
byte buffer[] = new byte[BUFFER_SIZE];

Next a stream is opened to the URL.

InputStream s = url.openStream();
int size = 0;

The contents of the URL are downloaded and appended to the "result" buffer.

do
{
  size = s.read(buffer);
  if (size != -1)
    result.append(new String(buffer, 0, size));
} while (size != -1);

This process continues until a -1 is returned from the length. This signifies that we have reached the end of the data.

return result.toString();

Finally, the results are returned as a String.

Copyright 2005-2009 by Heaton Research, Inc.