Vous êtes sur la page 1sur 10

ASP Request Object

Theory Section
ASP Request Object
The Request object retrieves the values that the client browser
passed to the server during an HTTP request. It is used to get information
from the user. Using this object, you can dynamically create web pages
and perform various server-side actions based on input from the user.

Its collections, properties and methods are described below:

Collections

Request Object consists of five collections, these are:

 ClientCertificate
 Cookies
 Form
 QueryString
 ServerVariables

Properties
Request Object consists of one property i.e.,

 TotalBytes

Methods

Request Object consists of one method i.e.,

 BinaryRead

Collections
ClientCertificate

The ClientCertificate collection provides access to the certification


fields from a request issued by the web browser. Commonly, it is used
when the client is requesting secure pages through SSL connection. Before
using this collection, you must configure your Web server to request client
certificates.

Cookies

The Cookies collection is used to set or get cookie values. If the


cookie does not exist, it will be created, and take the value that is
specified.

Note: The Response.Cookies command must appear before the <html>


tag.

Syntax:

Response.Cookies(name)[(key)|.attribute]=value
variablename=Request.Cookies(name)[(key)|.attribute]

name Required. The name of the cookie


value Required for the Response.Cookies command. The Value of
the cookie
attribute Optional. Specifies information about the cookie
key Optional. Specifies the key where the value is assigned

Form

The Form collection is used to retrieve the values of form elements


from a form that uses the POST method.

Syntax
Request.Form(element)[(index)|.Count]

element Required. The name of the form element from which the
collection is to retrieve values
index Optional. Specifies one of multiple values for a
parameter.From 1 to Request.Form(parameter).Count.

QueryString

The QueryString collection is used to retrieve the variable values in


the HTTP query string.

The HTTP query string is specified by the values following the question
mark (?), like this:

<a href= "test.asp?txt=this is a query string test">Link with a query


string</a>

The line above generates a variable named txt with the value "this is a
query string test".
Query strings are also generated by form submission, or by a user typing a
query into the address bar of the browser.

Note: If you want to send large amounts of data (beyond 100 kb) the
Request.QueryString cannot be used.

Syntax:
Request.QueryString(variable)[(index)|.Count]

variable Required. The name of the variable in the HTTP query string
to retrieve
index Optional. Specifies one of multiple values for a variable. From
1 to Request.QueryString(variable).Count

ServerVariables

The ServerVariables collection is used to retrieve the server variable


values.

Syntax:
Request.ServerVariables (server_variable)

server_variabl Required. The name of the server variable to retrieve


e

Properties
TotalBytes

The TotalBytes property is a read-only property that returns the


total number of bytes the client sent in the body of the request.

Syntax:
varbytes=Request.Totalbytes

Methods
BinaryRead

The BinaryRead method is used to retrieve the data sent to the


server from the client as part of a POST request. It will store the data in a
safe array (an array that stores information about the number of
dimensions and the bounds of its dimensions).

Note: A call to Request.Form after a call to BinaryRead, and vice-versa, will


cause an error.

Syntax:
Request.BinaryRead(count)

count Required. Specifies how many bytes to read from the client
ASP Request Object

Example Section
Collections

Client Certificate

Example:

You can iterate through the keys of the ClientCertificate collection, as


shown in the following example.

<%
For Each strKey in Request.ClientCertificate
Response.Write strKey & " = " & Request.ClientCertificate(strKey) &
"<BR>"
Next
%>

Cookies

Example:

<%
dim numvisits
response.cookies("NumVisits").Expires=date+365
numvisits=request.cookies("NumVisits")

if numvisits="" then
response.cookies("NumVisits")=1
response.write("Welcome! This is the first time you are visiting this Web
page.")
else
response.cookies("NumVisits")=numvisits+1
response.write("You have visited this ")
response.write("Web page " & numvisits)
if numvisits=1 then
response.write " time before!"
else
response.write " times before!"
end if
end if
%>
<html>
<body>
</body>
</html>

Form
Example:

<html>
<body>
<form action="demo_simpleform.asp" method="post">
Your name: <input type="text" name="fname" size="20" />
<input type="submit" value="Submit" />
</form>
<%
dim fname
fname=Request.Form("fname")
If fname<>"" Then
Response.Write("Hello " & fname & "!<br />")
Response.Write("How are you today?")
End If
%>
</body>
</html>

QueryString

<html>
<body>

<%
If Request.QueryString<>"" Then
If Request.QueryString("name")<>", " Then
name1=Request.QueryString("name")(1)
name2=Request.QueryString("name")(2)
end if
end if
%>

<form action="demo_reqquery2.asp" method="get">


First name:
<input type="text" name="name" value="<%=name1%>" />
<br />
Last name:
<input type="text" name="name" value="<%=name2%>" />
<br />
<input type="submit" value="Submit" />
</form>
<hr>
<%
If Request.QueryString<>"" Then
Response.Write("<p>")
Response.Write("The information received from the form was:")
Response.Write("</p><p>")
Response.Write("name=" & Request.QueryString("name"))
Response.Write("</p><p>")
Response.Write("The name property's count is: ")
Response.Write(Request.QueryString("name").Count)
Response.Write("</p><p>")
Response.Write("First name=" & name1)
Response.Write("</p><p>")
Response.Write("Last name=" & name2)
Response.Write("</p>")
end if
%>
</body>
</html>

ServerVariables

Example:

<html>
<body>
<p>
<b>You are browsing this site with:</b>
<%Response.Write(Request.ServerVariables("http_user_agent"))%>
</p>
<p>
<b>Your IP address is:</b>
<%Response.Write(Request.ServerVariables("remote_addr"))%>
</p>
<p>
<b>The DNS lookup of the IP address is:</b>
<%Response.Write(Request.ServerVariables("remote_host"))%>
</p>
<p>
<b>The method used to call the page:</b>
<%Response.Write(Request.ServerVariables("request_method"))%>
</p>
<p>
<b>The server's domain name:</b>
<%Response.Write(Request.ServerVariables("server_name"))%>
</p>
<p>
<b>The server's port:</b>
<%Response.Write(Request.ServerVariables("server_port"))%>
</p>
<p>
<b>The server's software:</b>
<%Response.Write(Request.ServerVariables("server_software"))%>
</p>

</body>
</html>

Properties

TotalBytes

Example:
The following code sets the variable a equal to the total number of bytes
sent in the body of the request:

<%
dim a
a=Request.TotalBytes
Response.Write(“The Total Bytes sent is” & a)
%>

Methods
BinaryRead

Example;

The following example uses the BinaryRead method to place the content
of a request into a safe array:
<%
dim a, b
a=Request.TotalBytes
b=Request.BinaryRead(a)
%>
End of Project

Vous aimerez peut-être aussi