Skip to content

Instantly share code, notes, and snippets.

@scriptnull
Created December 8, 2013 16:35
Show Gist options
  • Save scriptnull/7859878 to your computer and use it in GitHub Desktop.
Save scriptnull/7859878 to your computer and use it in GitHub Desktop.
parse a webpage into a single String . This can be used in future for manipulating json data .
package mygists;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
public class GetWebPageAsString {
public static void main(String[] args) {
String str = getStringFromWeb("http://localhost:8080/");
System.out.println(str);
}
public static String getStringFromWeb(String urladdress) {
String res = new String();
try {
URL url;
InputStream is = null;
BufferedReader br;
String line;
url = new URL(urladdress);
is = url.openStream(); // throws an IOException
br = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
line = br.readLine();
while ( line != null) {
sb.append(line);
line = br.readLine();
}
res = sb.toString();
} catch(Exception e)
{
System.out.println(e);
}
return res ;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment