CS 325 Assignment #2

Fetching Files from an HTTP Server

Due September 22nd by 11:59pm
(Not accepted after 9/24)

Introduction:

This assignment will give you first-hand experience with the application layer of the network stack, and its interface with the layer below. You'll write, in Java, a simple HTTP client that fetches a single document from an HTTP server and writes it to a file. The hostname, document path, and filename will all be passed as command-line arguments. Your program is responsible for opening the TCP connection, sending a well-formatted HTTP request, and retrieving the data payload from the response. You'll therefore be using an application-layer protocol (HTTP), and also becoming more familiar with the interface between the application layer and the services offered by the layers below.

Specifics:

Sample Output

Here's some sample output from runs of my program. (My Java class is called Fetch.) I'm interacting with it on the command line to make it easier to demonstrate how it responds. You're welcome to run and test yours from within an IDE, though you'll need to edit the Run Configuration (if using eclipse) so that arguments are passed to your program when it runs.

The first run of my program shows how it behaves if it doesn't get the three inputs it's expecting: It reminds the user about the arguments it's expecting. I then fetch the short HTML page from the first wireshark lab. (The cat command displays the contents of a file.) I then download a .jpg file and use ls to show its size. Yours should also be 23,556 bytes long, and should display properly when opened in your favorite .jpg viewer. Finally, I show what happens if I try to reach a host that doesn't exist, and if I request a file that doesn't exist.

brichards[100] : javac Fetch.java 
brichards[101] : java Fetch
Usage: <hostname> <resource> <filename>
brichards[102] : java Fetch gaia.cs.umass.edu /wireshark-labs/INTRO-wireshark-file1.html message.html
Grabbing /wireshark-labs/INTRO-wireshark-file1.html from gaia.cs.umass.edu
Writing data to /Users/brichards/message.html
brichards[103] : cat message.html
<html>
Congratulations!  You've downloaded the first Wireshark lab file!
</html>
brichards[104] : java Fetch cs.pugetsound.edu /~brichards/sound.jpg image.jpg
Grabbing /~brichards/sound.jpg from cs.pugetsound.edu
Writing data to /Users/brichards/image.jpg
brichards[105] : ls -l image.jpg 
-rw-r--r--  1 brichards  staff  23556 Sep 10 12:36 image.jpg
brichards[106] : java Fetch brad.pugetsound.edu /index.html output.txt
Grabbing /index.html from brad.pugetsound.edu
Error: Host brad.pugetsound.edu is unreachable
brichards[107] : java Fetch cs.pugetsound.edu /~brichards/bogus.html output.html
Grabbing /~brichards/bogus.html from cs.pugetsound.edu
Writing data to /Users/brichards/output.html
brichards[108] : cat output.html 
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>404 Not Found</title>
</head><body>
<h1>Not Found</h1>
<p>The requested URL was not found on this server.</p>
</body></html>

Hints & Tips:

Submitting:

Submit your .java file (and only your .java file) via Canvas.


Brad Richards, 2023