Skip to content

Instantly share code, notes, and snippets.

@akunzai
Last active August 29, 2015 14:00
Show Gist options
  • Save akunzai/7d64e688b779d5315d82 to your computer and use it in GitHub Desktop.
Save akunzai/7d64e688b779d5315d82 to your computer and use it in GitHub Desktop.
A simple Java class to provide functionality similar to Wget
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