Posted By

gdvickery on 03/12/09


Tagged

http java read Website


Versions (?)

Who likes this?

4 people have marked this snippet as a favorite

appx
cygni
vkolev
studioevoque


Read a web page with Java


 / Published in: Java
 

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

  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  

You need to login to post a comment.