Java Reference
In-Depth Information
import java.net.*;
public class GetPage
{
/**
* 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();
}
/**
* Run the example.
*
* @param page The page to download.
*/
public void go(String page)
{
try
{
Search WWH ::




Custom Search