Java Reference
In-Depth Information
All of the work performed by this program is done inside of the scan method. The
first thing that the scan method does is to create a new URL object and then create an
HttpURLConnection object from there. The following lines of code do this:
URL url = new URL(u);
HttpURLConnection http = (HttpURLConnection)
url.openConnection();
Once the connection has been established, a few local variables are created to keep track
of the headers being displayed. The key variable will hold the name of each header found.
The value variable will hold the value of that header. The count variable keeps a count
of which header we are on.
int count = 0;
String key, value;
Next, a do/while loop will be used to loop through each of the headers.
do
{
key = http.getHeaderFieldKey(count);
value = http.getHeaderField(count);
We know that we have reached the end of the headers when we find a header that has a
null value. However; a null key is acceptable, the first header always had a null key,
because, as you recall from Chapter 1 and 2, the first header is always in a different format.
count++;
if (value != null)
{
If there is no key value, then just display the value. If both are present, then display both.
You will never have a key, but no value.
if (key == null)
System.out.println(value);
else
System.out.println(key + ": " + value);
The loop continues until a value with the value of null is found.
}
} while (value != null);
This process will continue until all headers have been displayed.
Search WWH ::




Custom Search