Cross-Platform, Java

Get URL contents with Java

It is very easy to download url contents using a few lines of java code:

The source code below is licensed under GPLv3.

GetURLContents.java (GetURLContents)

import java.io.*;
import java.net.*;
 
public class GetURLContents {
 
   public static void main (String[] args) {
      URL url;
      InputStream input = null;
      BufferedReader inputs;
      String str;
 
      try {
         url = new URL("http://google.com/");
         input = url.openStream();
         inputs = new BufferedReader(new InputStreamReader(input));
 
         // save contents in a file instead of printing them to the console
         while ((str = inputs.readLine()) != null) {
            System.out.println(str);
         }
 
      } catch (MalformedURLException mue) {
         System.err.println("Error (MalformedURLException): " + mue.getMessage());
         System.exit(1);
 
      } catch (IOException ioe) {
         System.err.println("Error (IOException): " + ioe.getMessage());
         System.exit(1);
 
      } finally {
         try {
            input.close();
         } catch (IOException ioe) {}
      }
   }
}