Fetch the contents of webpage via Rhino


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



Copy this code and paste it in your HTML
  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


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.