Java Reference
In-Depth Information
The above command simply shows the abstract format to call this recipe, with the appro-
priate parameters. For exact information on how to run this recipe refer to Appendix B, C, or
D, depending on the operating system you are using.
After running the above command, the contents of
http://www.httprecipes.com will now be displayed to the console, instead of
http://www.httprecipes.com/1/3/time.php .
This recipe provides one very useful function. The downloadPage function, shown
here:
public String downloadPage(URL url) throws IOException
This function accepts a URL , and downloads the contents of that web page. The contents
are returned as a string. The implementation of the downloadPage function is some-
what simple, and follows the code already discussed in this chapter.
This recipe can be applied to any real-world site that contains data on a single page for
which you wish to download the HTML.
Once you have the web page downloaded into a string, you may be wondering what you
can do with the data. As you will see from the next recipe, you can extract information from
that page.
Recipe #3.2: Extract Simple Information from a Web Page
If you need to extract simple information from a web page, this recipe can serve as a good
foundation for a more complex program. This recipe downloads the contents of a web page
and extracts a piece of information from that page. For many tasks, this recipe will be all that
is needed. This is particularly true, if you can get to the data directly from a URL and do not
need to log in, or pass through any intermediary pages.
This recipe will download the current time for the city of St. Louis, MO. To do this it will
use the following URL:
http://www.httprecipes.com/1/3/time.php
The above URL is one of the examples on the HTTP recipes web site. The contents of this
page are shown in Listing 3.2. The piece of data that we would like to extract from Figure 3.2
is the current date and time. Figure 3.2 shows exactly what the web page looks like to a user.
For exact information on how to run this recipe refer to Appendix B, C, or D, depending on
the operating system you are using.
Search WWH ::




Custom Search