Skip to content

Instantly share code, notes, and snippets.

@eloipuertas
Created May 6, 2014 20:33
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/29cde8dd866a06e7f33a to your computer and use it in GitHub Desktop.
Save eloipuertas/29cde8dd866a06e7f33a to your computer and use it in GitHub Desktop.
HTTPClient
// MyStreamSocket is a Java class presented in Chapter 4.
// import MyStreamSocket;
import java.net.*;
import java.io.*;
public class HTTPClient {
// An application which communicates with a HTTP
// server to retrieve 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 {
InetAddress host =
InetAddress.getByName(args[0]);
int port = Integer.parseInt(args[1]);
String fileName = args[2].trim();
String request =
"GET " + fileName + " HTTP/1.0\n\n";
MyStreamSocket mySocket =
new MyStreamSocket(host, port);
/**/ System.out.println("Connection made");
mySocket.sendMessage(request);
// now receive the response from the HTTP server
String response;
response = mySocket.receiveMessage();
// read and display one line at a time
while (response != null) {
System.out.println(response);
response = mySocket.receiveMessage();
}
} // end try
catch (Exception ex) {
System.out.println("ERROR : " + ex) ;
ex.printStackTrace(System.out);
} // end catch
}// end else
}// end main
}
class MyStreamSocket extends Socket {
private Socket socket;
private BufferedReader input;
private PrintWriter output;
MyStreamSocket(InetAddress acceptorHost,
int acceptorPort ) throws SocketException,
IOException{
socket = new Socket(acceptorHost, acceptorPort );
setStreams( );
}
MyStreamSocket(Socket socket) throws IOException {
this.socket = socket;
setStreams( );
}
private void setStreams( ) throws IOException{
// get an input stream for reading from the data socket
InputStream inStream = socket.getInputStream();
input =
new BufferedReader(new InputStreamReader(inStream));
OutputStream outStream = socket.getOutputStream();
// create a PrinterWriter object for character-mode output
output =
new PrintWriter(new OutputStreamWriter(outStream));
}
public void sendMessage(String message)
throws IOException {
output.print(message + "\n");
//The ensuing flush method call is necessary for the data to
// be written to the socket data stream before the
// socket is closed.
output.flush();
} // end sendMessage
public String receiveMessage( )
throws IOException {
// read a line from the data stream
String message = input.readLine( );
return message;
} //end receiveMessage
public void close( )
throws IOException {
socket.close( );
}
} //end class
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment