Java Reference
In-Depth Information
Recipes
This chapter includes four recipes. These four recipes will demonstrate the following:
• Scanning a URL for headers
• Searching a range of IP addresses for web sites
• Downloading a binary or text file
• Monitoring a site to see that it stays up
These recipes will introduce you to some of the things that can be done with the
HttpURLConnection class.
Recipe #4.1: Scan URL
Sometimes it is helpful to examine the headers for a particular URL. Recipe 4.1 shows
how to use the HttpURLConnection class to access the headers for a particular URL.
This program is shown in Listing 4.1.
Listing 4.1: Scan a URL for HTTP Response Headers (ScanURL.java)
package com.heatonresearch.httprecipes.ch4.recipe1;
import java.io.IOException;
import java.net.*;
public class ScanURL
{
/**
* Scan the URL and display headers.
*
* @param u The URL to scan.
* @throws IOException Error scanning URL.
*/
public void scan(String u) throws IOException
{
URL url = new URL(u);
HttpURLConnection http =
(HttpURLConnection) url.openConnection();
int count = 0;
String key, value;
do
{
key = http.getHeaderFieldKey(count);
value = http.getHeaderField(count);
count++;
if (value != null)
Search WWH ::




Custom Search