BA372 - Assignment 1: HTTP Image Browser
In C# write a simple HTTP image browser; i.e., a Windows forms app which, like an
ordinary Web browser, allows users to enter a URL in a textbox at the
top of the window, makes an HTTP request for the resource located at the URL
and which, if the URL references an image file; e.g., *.jpg, *.gif, *.png, etc., displays the retrieved image in the
However, this browser should not only
display the image, it should also, in a small window above the image,
display the HTTP response line and all HTTP headers
received from the server serving the HTTP request. For instance, an HTTP
request for http://classes.bus.oregonstate.edu/ba371/reitsma/images/cob.png,
results in the following response line and headers being returned by COB's Web server:
At this point in our coursework, we should also start thinking a little about user interface issues.
For instance, just like users can enlarge or shrink their Web browser window, so should your user be able to
enlarge and shrink the window of your HTTP image browser. Obviously, with such enlarging/shrinking, the
interface elements contained in the window should enlarge/shrink accordingly.
Your program should also recognize whether or not an image was
requested and if the request actually succeeded. If not, an error message should gracefully handle the error.
Similarly, your program should recognize if a user entered an invalid URL.
Also, make sure that you build in a facility for handling timeouts;
i.e., handling an HTTP server
that does not reply within a certain amount of time.
Finally, make it so that, just as in a regular browser, you can hit the Enter/Return key on the keyboard instead of having to click the
HTTP/1.1 200 OK
Last-Modified: Wed, 22 Nov 2017 21:46:08 GMT
Date: Mon, 19 Mar 2018 23:39:02 GMT
Length: 59587 (58K) [image/png]
In addition, we want to distinguish the HTTP response status from
the headers and display it in a separate Status code
textbox (see example above).
Clearly, since we do not know how many headers we will be receiving
from the server, we must make the box that lists the returned
headers scrollable. Similarly, we want to accommodate for large images. Hence, we want some
way of scaling the images or be able to scroll them.
Some tips and hints:
- You instructor is very aware of the fact that you have not practiced HTTP requests in a lab. However, at this point in
your training you should be able to 'make Google your friend' and be able to formulate Google queries which are both targeted and precise enough
to give you few but very good search results. For instance, in the next tip/hint, you are advised to use the
System.Net.HttpWebRequest class for
forming a C# HTTP request. You might ask Google for a simple or basic example on how to use that
class: e.g., c# HttpWebRequest "simple example" (the double quotes are important!!)
- To make the HTTP request (using the Uri class),
you might want to use the System.Net.HttpWebRequest class.
- An HTTP timeout (in milliseconds) can be set by using the
attribute of the
HttpWebRequest. A five second (5,000 milliseconds) is a good default.
- To get a hold of the HTTP response, you might want to use the System.Net.HttpWebResponse class.
- These HttpWebRequest and HttpWebResponse classes are very rich and powerful. For instance, almost
anything you want to know about the HTTP response (its status, does the body contain an image? how many bytes were returned? what are its headers? etc.) is stored on the HttpWebResponse.
- Whenever we have to display images, we have a potential scaling problem; the image is
either larger or smaller than the canvas on which we want to display it. Although several ways
to deal with the problem exist, a simple (quick and dirty) way to accommodate large images
is to embed the PictureBox on which we display the image inside a
Panel object and let the
scroll the PictureBox. If we make the
PictureBox object larger than the
Panel object, set its
Autosize and set the
Autoscroll property to
Panel will scroll the
- There exist several ways to display an image in a PictureBox.
However, do not use the PictureBox.Load(url) method
as this would generate a brand new HTTP request. This would make little sense since you already made the request and already have the image in memory.
An alternative method is to stream the image from memory directly to the PictureBox's
Image like so:
System.IO.Stream my_stream = http_response.GetResponseStream();
myPictureBox.Image = Image.FromStream(my_stream);
To make the Enter/Return key work as the Go button, we can do several things.
A relatively easy and elegant solution is to write code for the _KeyDown method of the URL textbox
which checks to see if the key pressed is the Enter/return key, and if so, calls the
method which is normally invoked by the Go button. Something like the following should work:
private void URLTextBox_KeyDown(Object sender, KeyEventArgs e)
if (e.KeyCode == Keys.Enter)
goButton_Click(URLTextBox, new EventArgs());