The ASP Request object has information about the current request from the client to the server. However it also gets info about the server too.

Basic Usage

The ASP Request object retrieves all sorts of information from the client (ie browser user) whenever a client makes an HTTP request. The different types of information are variables grouped into collections:

It is possible to retrieve any variable by using Request(variable) syntax but it is faster and more explicit to use Request.Collection(variable) syntax.

The Request collections can be looped through like most other collections. Here is the syntax:

For Each Key in Request.Collection
    ...
Next
For intItem = 1 to Request.Collection.Count
    ...
Next
for (var i=1; i<=Request.Form.Count; i++) {
    Response.Write(i+"
"); Response.Write(Request.Form(i).Item+"
"); }

Characters that don't transfer well in URLs are encoded with this syntax: %XX (if non-alphanumeric ASCII) or %uXXXX (if non-ASCII). Here is a table of relevant special characters in the URL:

Special
character
Special
meaning
Hexadecimal
encoding
+ Indicates a space (spaces cannot be used in a URL). %20
# Indicates bookmarks. %23
% Specifies special characters. %25
& Separator between parameters specified in the URL. %26
/ Separates directories and subdirectories. %2F
? Separates the actual URL and the parameters. %3F

EG General

This example demonstrates the ServerVariables, Form, and QueryString collections.

If Request.ServerVariables("REQUEST_METHOD") = "POST" Then
  strName = Request.Form("txtName")
Else 'The form submit method was GET
  strName = Request.QueryString("Sequence")
End If

EG QueryString

This example shows what a QueryString in the URL of a client's address bar might look like. The space ( ) is parsed as %20, i.e. 32 decimal in ASCII.

http://your.com/results.asp?var1=Hello%20world!&var2=checked

EG BinaryRead

It is possible to get raw data (unsigned bytes) from a page with Reqeust.BinaryRead:

vntPostedData = Request.BinaryRead(Request.TotalBytes)

Collections

ClientCertificate

X.509 standard security variables from the client's certificate when requests are made with https:// (instead of http://) in the URL and the web server has been configured for the SSL3.0/PCT1 protocol. The following variables can be retrieved:

Cookies

Variables from cookies on the client's machine can be retrieved.

Request.Cookies(cookie)
'Returns all Key=Value pairs in query string format.
Request.Cookies(cookie)(key) 'Returns value for specified key in a cookie.
Request.Cookies(cookie).HasKeys
'Returns true if the cookie has keys. 'Read-only.

To set cookies, see also Response.

Form

Data from the client sent via an HTML form. Used with HTML form method="post".

Request.Form'Returns data from all form elements in query string format.
Request.Form(element)
'Returns value from specfied element in form.
For i = 1 To Request.Form("checkboxElement").Count
    Response.Write Request.Form("checkboxElement")(i) & "<br />" 'A checkbox may have 0-N values 'These are 1-based not 0-based, even in JS
Next
Dim Item
For Each Item in Request.Form
    Response.Write("<p>" & Item & ": " & Request.Form(Item).Item & ".</p>" & Chr(10))
    'Shows collected field names and field values.
Next

Click here for an fuller example of Handling Forms.

QueryString

Info from the user that is attached to the URL. Used with HTML form method="get". The data is attached to the URL with a ?, followed by key and value pairs concatenated with &. The full URL (including everything before the query string) has a max of 2083 bytes.

Request.QueryString
'Returns the whole query string.
Request.QueryString(key)
'Returns value from specified key.
For Each item In Request.QueryString("key")
    Response.Write Request.QueryString("key")(item) & "<br />"
 'A key may occur multiple time in a query string.
Next

It is common to check for the existence of a querystring variable. This is very finicky because of the different way of "nothing" (null, undefined, and empty string just for starters).

'In VBScript
Dim strMessage
If IsNull(Request.QueryString("msg")) or IsEmpty(Request.QueryString("msg")) Then
    strMessage = ""
Else
    strMessage = Request.QueryString("msg")
End If
If Len(strMessage) > 0 Then
    Response.Write strMessage 
End If
//In Javascript
var Message=String(Request.QueryString("msg"));
if ((Message.length>0)&&(Message!="undefined")) Response.Write(Message);

ServerVariables

Variables regarding predetermined environment variables.

Request.ServerVariables("ServerEnvironmentVariable")
'Returns specified server environment variable.

Click here for a fuller example of Server Variables.

Here are some of the nicer environment variables that can be retrieved:

Properties

TotalBytes

Read only. The number of bytes sent by the client in the request.

Methods

BinaryRead(count)

Gets an array of unsigned bytes from the client as part of a POST request. The length of the array (count) must be less than or equal to the Request.TotalBytes. Request.BinaryRead() and Request.Form are exclusive, that is you can use one but not the other in any given ASP page.

2008-06-25 17:21:07Z