Read a web page with Java


/ Published in: Java
Save to your folder(s)

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


Copy this code and paste it in your HTML
  1. public String readURL(String address) throws Exception
  2. {
  3. URL url = new URL(address);
  4. HttpURLConnection conn = (HttpURLConnection)url.openConnection();
  5. conn.setReadTimeout(5000);
  6. conn.setConnectTimeout(5000);
  7.  
  8. byte[] buff = new byte[1024];
  9. InputStream in = conn.getInputStream();
  10. int read;
  11.  
  12. while((read = in.read(buff)) != -1)
  13. out.write(buff, 0, read);
  14.  
  15. return out.toString().replaceAll("[\\s]+", " ");
  16. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.