Last active
August 29, 2015 14:00
-
-
Save akunzai/7d64e688b779d5315d82 to your computer and use it in GitHub Desktop.
A simple Java class to provide functionality similar to Wget
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.*; | |
import java.net.*; | |
public class JGet { | |
public static void main(String[] args) { | |
if ( (args.length == 0) ){ | |
System.err.println( "\nUsage: java HttpGet [urlToGet]" ); | |
System.exit(1); | |
} | |
String url = args[0]; | |
URL u; | |
InputStream is = null; | |
BufferedReader in; | |
String s; | |
try { | |
u = new URL(url); | |
is = u.openStream(); | |
in = new BufferedReader(new InputStreamReader(is)); | |
while ((s = in.readLine()) != null){ | |
System.out.println(s); | |
} | |
} | |
catch (MalformedURLException mue){ | |
System.err.println("Ouch - a MalformedURLException happened."); | |
mue.printStackTrace(); | |
System.exit(2); | |
} | |
catch (IOException ioe){ | |
System.err.println("Oops - an IOException happened."); | |
ioe.printStackTrace(); | |
System.exit(3); | |
} | |
finally{ | |
try{ | |
is.close(); | |
} | |
catch (IOException ioe){ | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment