Sunday, June 26, 2011

Read a web page with Java !

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]+", " ");
    }


Shortest Java program to do wget:

 public static void main(String[] args) throws Exception
    {
        String s;
        BufferedReader r = new BufferedReader(new InputStreamReader(new URL(args[0]).openStream()));
        while ((s = r.readLine()) != null)
        {
            System.out.println(s);
        }
    }

No comments:

Post a Comment