We Recommend

Pro JavaScript Techniques Pro JavaScript Techniques
Pro JavaScript Techniques is the ultimate JavaScript book for the modern web developer. It provides everything you need to know about modern JavaScript, and shows what JavaScript can do for your web sites. This book doesn't waste any time looking at things you already know, like basic syntax and structures.


Posted By

micmath on 03/11/08


Tagged

rhino


Versions (?)


Fetch the contents of webpage via Rhino


Published in: JavaScript 


  1. /**
  2.  * Get the contents returned from a web URL.
  3.  * @param {string} urlStr The web address of the page you wish to fetch.
  4.  * @returns {string} The contents of the web page.
  5.  */
  6. function fetch(urlStr) {
  7. if (typeof java == "undefined") {
  8. throw "This script requires java to run.";
  9. }
  10. else {
  11. importPackage(java.io, java.net);
  12.  
  13. var url = new URL(urlStr);
  14. var urlStream = url.openStream();
  15. var reader = new BufferedReader(new InputStreamReader(urlStream, "latin1"));
  16.  
  17. var html = "";
  18. var line;
  19. while (line = reader.readLine()) {
  20. if (line == null) break;
  21. html += line;
  22. }
  23. return html;
  24. }
  25. }

Report this snippet 

You need to login to post a comment.