Java open url with URL and URLConnection.

URL url = new URL("http://www.example.com");
URLConnection urlConnection = url.openConnection();
// without UA, some cdn like cloudflare will block the request with 403
urlConnection.setRequestProperty("User-Agent", "Chrome/50");
BufferedReader reader = new BufferedReader(
    new InputStreamReader(urlConnection.getInputStream())
);
while (reader.ready()) {
    System.out.println(reader.readLine());
}
TOPâ–˛
Post tagged with: java