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.