Created
May 6, 2014 20:40
-
-
Save eloipuertas/0f67a01d38dfd999122f to your computer and use it in GitHub Desktop.
URLBrowser
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.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