Game Development Reference
HttpWebRequest and HttpWebResponse
The .NET framework provides the abstract class WebRequest , which is the request
and response model for accessing data from the Internet. This model is protocol-
agnostic, specialized by classes inheriting from the abstract class. There are a variety
of specialized descendents of WebRequest , like FileWebRequest for handling file://
paths, but this chapter will focus on the HTTP protocol using HttpWebRequest .
The HTTP protocol is the primary transport mechanism for communicating with
Internet resources. A developer may use this mechanism to download application
updates and configuration information that constantly changes or even to post
messages to a dynamic environment like an ASP.NET application. The HttpWebRequest
class implements the WebRequest class, providing a specialized request class to com-
municate over the HTTP protocol. This class enables an application to interact
directly with servers using HTTP.
Server resources are identified by uniform resource identifiers (URI), and the
.NET framework provides the Uri class, which defines the properties and methods
for handling uniform resource identifiers, such as comparing, combing, and pars-
ing. Requests are sent from an application to a URI, such as a zipped fie or web
page. The requests are sent using HttpWebRequest to the remote server, using the
HTTP protocol as the transport mechanism to access the resource.
If an error occurs with a request, a WebException is thrown that contains details about why
the request failed. The Status property is of type WebRequestStatus , and if the value is
WebRequestStatus.ProtocolError , the response returned from the server is contained in the
The remainder of the chapter will cover the construction of an asynchronous
wrapper around HttpWebRequest and HttpWebResponse .
The Request Object
The first component of our asynchronous wrapper is the request object, which
serves as the public interface between the application and the rest of the wrapper.
This wrapper executes the core system and fires a download complete event when
the file has finished downloading. There is also a progress update event that you
can subscribe to in order to display download progress to the users.
public class AsyncFileDownloadRequest