How to download a website source in Java

To download a website source in Java you can use the following snippet.

Sample Java

private static String getUrlSource(String url) throws IOException {
	URL website = new URL(url);
	URLConnection connection = website.openConnection();
	BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
	String input;
	StringBuilder strBuilder = new StringBuilder();
	while ((input = bufferedReader.readLine()) != null){
			strBuilder.append(input);
	}
	bufferedReader.close();
	return strBuilder.toString();
}

One thought on “How to download a website source in Java

Leave a Reply