Here's a short method that nicely wraps these details (based on java.util.Scanner
):
public static String get(String url) throws Exception {
StringBuilder sb = new StringBuilder();
for(Scanner sc = new Scanner(new URL(url).openStream()); sc.hasNext(); )
sb.append(sc.nextLine()).append('\n');
return sb.toString();
}
And this is how it is used:
public static void main(String[] args) throws Exception {
System.out.println(get("http://www.yahoo.com"));
}