Java Reference
In-Depth Information
Listing 9.1: Automatic Choice Lists (DownloadAtricle.java)
package com.heatonresearch.httprecipes.ch9.recipe1;
import java.io.*;
import java.net.*;
import com.heatonresearch.httprecipes.html.*;
public class DownloadArticle
{
/**
* The size of the download buffer.
*/
public static int BUFFER_SIZE = 8192;
/**
* This method downloads the specified URL into a Java
* String. This is a very simple method, that you can
* reused anytime you need to quickly grab all data from
* a specific URL.
*
* @param url The URL to download.
* @return The contents of the URL that was downloaded.
* @throws IOException Thrown if any sort of error occurs.
*/
public String downloadPage(URL url) throws IOException
{
StringBuilder result = new StringBuilder();
byte buffer[] = new byte[BUFFER_SIZE];
InputStream s = url.openStream();
int size = 0;
do
{
size = s.read(buffer);
if (size != -1)
result.append(new String(buffer, 0, size));
} while (size != -1);
return result.toString();
}
/**
* This method is very useful for grabbing information from a
* HTML page.
Search WWH ::




Custom Search