Skip to content

Instantly share code, notes, and snippets.

@Jecvay
Created October 16, 2014 13:17
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 Jecvay/7504ac96ef085df91283 to your computer and use it in GitHub Desktop.
Save Jecvay/7504ac96ef085df91283 to your computer and use it in GitHub Desktop.
import java.net.*;
import java.io.*;
/**
* Class <em>Client</em> is a class representing a simple
* HTTP client.
*
* @author iCarnegie, Inc (CTE)
* @version 1.0
*/
public class Client
{
/**
* default HTTP port is port 80
*/
private static int port = 80;
/**
* Allow a maximum buffer size of 192 bytes
*/
private static int buffer_size = 8192;
/**
* The end of line character sequence.
*/
private static String CRLF = "\r\n";
/**
* Input is taken from the keyboard
*/
static BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));
/**
* Output is written to the screen (standard out)
*/
static PrintWriter screen = new PrintWriter (System.out, true);
public static void main( String [] args ) {
try {
/**
* Create a new HttpClient object.
*/
HttpClient myClient = new HttpClient();
/**
* Parse the input arguments.
*/
if (args.length != 1) {
System.err.println("Usage: Client <server>");
System.exit(0);
}
/**
* Connect to the input server
*/
myClient.connect( args[0] );
/**
* Read the get request from the terminal.
*/
screen.println(args[0] + " is listening to your request:");
String request = keyboard.readLine();
/**
* Be sure to add the necessary end-of-line characters to
* the request.
*/
request += CRLF + CRLF;
/**
* Ask the client to process the GET request.
*/
myClient.processGetRequest( request );
/**
* Get the headers and display them.
*/
screen.println( "Header: \n" );
screen.print( myClient.getHeader() + "\n" );
screen.flush();
/**
* Ask the user to input a name to save the resultant web page.
*/
screen.println();
screen.print("Enter the name of the file to save: ");
screen.flush();
String filename = keyboard.readLine();
FileOutputStream outfile = new FileOutputStream(filename);
/**
* Save the response to the specified file.
*/
String response = myClient.getResponse();
outfile.write( response.getBytes() );
outfile.flush();
outfile.close();
/**
* Close the connection client.
*/
myClient.close();
} catch( Exception e ) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment