R rafiqsab Lurker Jul 13, 2011 #1 hi all, i have situation where i need to pass the html page source as a input to the javascript function. so please help me in reading the html page source. thanks and regards rafiq
hi all, i have situation where i need to pass the html page source as a input to the javascript function. so please help me in reading the html page source. thanks and regards rafiq
dCoder Newbie Jul 13, 2011 #2 This method will return the html of a given URL as a String: public String getUrlContent(String urlStr) { try { URL url = new URL(urlStr); URLConnection con = null; con = url.openConnection(); String encoding = con.getContentEncoding(); if (encoding == null) { encoding = "ISO-8859-1"; } BufferedReader r = new BufferedReader(new InputStreamReader(con.getInputStream(), encoding)); StringBuilder sb = new StringBuilder(); try { String s; while ((s = r.readLine()) != null) { sb.append(s); sb.append("\n"); } } finally { r.close(); } return sb.toString(); } catch (IOException ex) { return ""; } }
This method will return the html of a given URL as a String: public String getUrlContent(String urlStr) { try { URL url = new URL(urlStr); URLConnection con = null; con = url.openConnection(); String encoding = con.getContentEncoding(); if (encoding == null) { encoding = "ISO-8859-1"; } BufferedReader r = new BufferedReader(new InputStreamReader(con.getInputStream(), encoding)); StringBuilder sb = new StringBuilder(); try { String s; while ((s = r.readLine()) != null) { sb.append(s); sb.append("\n"); } } finally { r.close(); } return sb.toString(); } catch (IOException ex) { return ""; } }