Mega Code Archive

 
Categories / Java Book / 006 Networking
 

0357 URL and URLConnection

Outputting the contents of the resource identified via a URL command-line argument import java.io.InputStream; import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; class Main { public static void main(String[] args) throws Exception { URL url = new URL("http://google.com"); try (InputStream is = url.openStream()) { int ch; while ((ch = is.read()) != -1) { System.out.print((char) ch); } } catch (Exception e) { e.printStackTrace(); } } }