Java Reference
In-Depth Information
}
catch(IOException e)
{
System.out.println("Could not connect to URL");
}
As you can see, the above code is a continuation of what we have already seen. Just as
in the previous code segments, the URL object is first created. Next a stream is opened to
the URL. Finally, a while loop is used to loop through and read from the stream, until there
is nothing more to read.
When reading from a stream, it is best to read the data in blocks, rather than one byte
at a time. To accomplish this, a buffer, of size 8,192, is used. The choice for 8,192 is purely
arbitrary. It is 8 kilobytes. Many web pages are under 8k, so they will be read in one block.
Once the read function returns -1 we know there is no data left to read.
Recipes
This chapter showed you how to use some of the basic HTTP functionality built into Java.
You have seen how you can use the URL class to open a stream to a web page. You have also
seen how to read the contents of the web page into a StringBuilder . The recipes for
this chapter will build upon this.
There are five recipes for this chapter. All of these recipes provide you with reusable
code that demonstrates the basic HTTP programming learned in this chapter. These recipes
will demonstrate the following functionality:
• Download the contents of a web page
• Extract data from a web page
• Pass parameters to a web page
• Parse time and date information
We will begin with recipe 3.1, which demonstrates how to download the contents of a
web page.
Recipe #3.1: Downloading the Contents of a Web Page
This recipe is the culmination of the example code given, up to this point, in this chapter.
Recipe 3.1 accesses a URL and downloads the contents into a StringBuilder . The
StringBuilder is then converted into a string and displayed.
This is shown in Listing 3.1.
Listing 3.1: Download a Web Page (GetPage.java)
package com.heatonresearch.httprecipes.ch3.recipe1;
import java.io.*;
Search WWH ::




Custom Search