Return to Snippet

Revision: 12368
at March 12, 2009 11:16 by gdvickery


Initial Code
public String readURL(String address) throws Exception
{
    URL url = new URL(address);
    HttpURLConnection conn = (HttpURLConnection)url.openConnection();
    conn.setReadTimeout(5000);
    conn.setConnectTimeout(5000);

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    byte[] buff = new byte[1024];
    InputStream in = conn.getInputStream();
    int read;

    while((read = in.read(buff)) != -1)
        out.write(buff, 0, read);

    return out.toString().replaceAll("[\\s]+", " ");
}

Initial URL


Initial Description
Connects to a web page, reads in the content and strips whitespace.

Initial Title
Read a web page with Java

Initial Tags
http, java

Initial Language
Java