F
package org.orteil.dashnetcookieclicker;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLConnection;
import java.net.UnknownHostException;
public class JavaDownloadWebPage {
public static void main(String[] args) {
try {
// Corrected URL with protocol
String url = "https://orteil.dashnet.org/cookieclicker/";
System.out.println("Attempting to access: " + url);
String result = downloadWebPage(url);
System.out.println(result);
} catch (UnknownHostException e) {
System.err.println("Unable to resolve host. Please check your internet connection or the URL.");
} catch (IOException | URISyntaxException e) {
System.err.println("An error occurred while trying to download the web page: " + e.getMessage());
}
}
private static String downloadWebPage(String url) throws IOException, URISyntaxException {
StringBuilder result = new StringBuilder();
String line;
// Convert the string URL to a URI and then to a URL
URLConnection urlConnection = new URI(url).toURL().openConnection();
urlConnection.addRequestProperty("User-Agent", "Mozilla");
urlConnection.setReadTimeout(5000);
urlConnection.setConnectTimeout(5000);
try (InputStream is = urlConnection.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is))) {
while ((line = br.readLine()) != null) {
result.append(line);
}
}
return result.toString();
}
}