Created
December 8, 2013 16:35
-
-
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 .
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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