How do I convert a web page to a String?
Author: Deron Eriksson
Description: This Java tutorial describes how to read a URL and convert it to a String.
Tutorial created using:
Windows XP || JDK 1.5.0_09 || Eclipse Web Tools Platform 1.5.1
The JavaSW URL class provides a convenient way of reading content from a URL. It is possible to open a connection to the URL, obtain an input stream, and then read that input stream. The input stream can be read into a StringBuffer, and this can be converted to a String. This technique is illustrated below. ConvertUrlToString.javapackage test; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; public class ConvertUrlToString { public static void main(String[] args) { try { String webPage = "http://www.google.com"; URL url = new URL(webPage); URLConnection urlConnection = url.openConnection(); InputStream is = urlConnection.getInputStream(); InputStreamReader isr = new InputStreamReader(is); int numCharsRead; char[] charArray = new char[1024]; StringBuffer sb = new StringBuffer(); while ((numCharsRead = isr.read(charArray)) > 0) { sb.append(charArray, 0, numCharsRead); } String result = sb.toString(); System.out.println("*** BEGIN ***"); System.out.println(result); System.out.println("*** END ***"); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } (Continued on page 2) |