Skip to content

Instantly share code, notes, and snippets.

@eloipuertas
Created May 6, 2014 20:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eloipuertas/0f67a01d38dfd999122f to your computer and use it in GitHub Desktop.
Save eloipuertas/0f67a01d38dfd999122f to your computer and use it in GitHub Desktop.
URLBrowser
import java.net.*;
import java.io.*;
public class URLBrowser {
// An application which uses a URL object to retreive
// the text contents of a web page.
// These command line arguments are expected, in order:
// <host name of the HTTP server>
// <port number of the HTTP server>
// <full path to a web document on the server host>
public static void main(String[] args) {
if (args.length != 3)
System.out.println
("This program requires 3 command line arguments");
else {
try {
String host = args[0];
String port = args[1].trim();
String fileName = args[2].trim();
String HTTPString =
"http://"+host+":"+port+"/"+fileName;
URL theURL = new URL(HTTPString);
InputStream inStream = theURL.openStream( );
BufferedReader input =
new BufferedReader
(new InputStreamReader(inStream));
String response;
response = input.readLine();
// read and display one line at a time
while (response != null) {
System.out.println(response);
response = input.readLine();
} //end while
}
catch (Exception ex) {
System.out.println("ERROR : " + ex) ;
ex.printStackTrace(System.out);
}
}// end else
}// end main
} //end class
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment