Chapter 16. The HttpRequest Class
The HttpRequest class is
ASP.NET's replacement for ASP's
Request intrinsic object. Because the HttpRequest
class instance for a given ASP.NET page is exposed as the Request
property of the Page class (from which all pages
are derived), you can code to the HttpRequest
class just as you did in ASP. Thus, your existing ASP code will be
that much easier to migrate.The HttpRequest class is used to access
information related to a particular HTTP request made by a web
client. The HttpRequest class provides access to
this information through its properties, collections, and methods.Each HTTP request from a client consists of an HTTP header and,
optionally, a body. The header and body are separated by a blank
line. The code following shows a typical HTTP request (without a
body):
GET /ASPdotNET_iaN/Chapter_16/showHTTP.aspx HTTP/1.0
Connection: Keep-Alive
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, image/png, */*
Accept-Charset: iso-8859-1,*,utf-8
Accept-Encoding: gzip
Accept-Language: en
Host: localhost
User-Agent: Mozilla/4.08 [en] (WinNT; U ;Nav)
The first line of the HTTP header contains the request type, followed
by a space, followed by the requested URL (URI), another space, and
the HTTP version number. In the previous example, the request type is
GET, the URL is
/ASPdotNET_iaN/Chapter_16/showHTTP.aspx (this
URL is relative to the server or domain name), and the HTTP version
is 1.0.
HTTP Request TypesThe current HTTP 1.1 standard (which can be found at http://www.w3.org/Protocols/rfc2616/rfc2616l) defines the valid request types for an HTTP request. These types are: While this list shows the valid request types, only the GET and HEAD are required to be supported by general-purpose servers. In practice, most, if not all, requests you'll deal with will be GET and POST type requests.GET requests simply ask the server to return a resource (such as an HTML or ASP.NET page) specified by the URL passed with the request. GET requests can also pass data to the server by appending it to the URL in the following format: GET /Chapter_16/showHTTP.aspx?name=andrew HTTP/1.0 This GET request fragment passes a key/value pair with the value "andrew" represented by the key "name." When more than one key/value pair is passed, each pair is separated by the ampersand (&) character. When using GET requests for passing data, in most cases, data passed with a GET request is limited to around 2K, which is limiting for complex or lengthy data. Pages using data passed by a GET request may be subject to alteration by a user before the request is made. Any data received via a GET request should be validated to ensure that processing or storing it will not cause an undesirable result.POST requests are used to post data to the server. Like GET requests, this data is passed as one or more key/value pairs, separated by ampersands. Unlike GET requests, the key/value pairs in a POST request are passed in the request body: POST /Chapter_16/showHTTP.aspx HTTP/1.0 |
header (which can be accessed either as a URL-encoded string in the
Headers collection or by saving the request to disk by using the
SaveAs method), having to parse the text each time we wanted to find
a particular piece of information would be a pain.The HttpRequest class does this work for us,
allowing us to deal only with the specific piece(s) of information
that we're interested in. Table 16-1 lists the properties, collections, and methods
exposed by the HttpRequest class.
