Detailed description of ASP built-in Object Request and Response

Source: Internet
Author: User
Tags microsoft iis
The request and response objects are the two most common built-in objects provided by ASP. Between a browser (or other user agent) and a Web server, information exchange in requests and responses can be accessed and managed through two built-in objects in ASP, these two objects are called request and response objects.

Almost all the work on the ASP page needs to access these two objects. Using these two objects will affect the efficiency and reliability of the page. Of course, their main purpose is to access the value that the user sends back to the server, that is, to obtain from the <form> segment of the HTML page or to the end of the URL as a query string, and create appropriate outputs to return to the user, and they can share many of the same factors. For example, both objects can use Cookies stored on the client computer.

Therefore, we divide the content into two separate parts (each object part), and first start with the information exchange between the client and the server, and then study each object.

The research content is:

· How the client communicates with the server to transmit web or other resources.

· Details of the request and response objects and their commonalities.

· How to access the corresponding value through a form and query string.

· How to read or create cookies and store them on the client's computer.

· What are the server variables? How to access and modify the HTTP header.

· Describes changes to other related items, such as the use of the customer's certificate.

  Communication between the client and the server

To save space, use the word "Browser" in the following content. However, you must remember that applications that can access web pages are not only browsers, but many special applications download web pages from websites, for example, special client programs designed for users with impaired vision or other users who have difficulty using a common browser. The most obvious example is the robot that the search engine uses to access websites on the web. All these factors are taken into account, including common web browsers. The correct word should be the user agent ).

  Page request Dialog

When a browser sends a page request to a Web site, it must clearly tell the server which page the browser is requesting. First, you need to establish a connection with the server through the domain name, and then provide the full path and name of the requested page. Why is full path and name required? Web is a borderless environment, so you must create a session ID for each customer (I will introduce how ASP can do this later ).

This means that every time the server sends a page to the customer, the server will completely forget the customer. Therefore, when a customer requests the next page, it is exactly the same as a new visitor. The server cannot remember this customer, so it cannot determine which page they requested last time. This is because you cannot use relative paths to provide a page, even if the page contains a relative link. For example:

<A href = "Download. asp"> Next Page </A>

The browser automatically creates a complete URL for the new page by using the domain and path of the current page, or using the <BASE> element in the <HEAD> segment of the page, tell the browser what the URL of all links to a page is. For example:

<Base herf = http://www.wrox.com/Store>

When you point the mouse to a page Link, you can see it in the browser's status bar. The path of the current page and the current domain name, base domain name, or base path have already been combined with the requested page name.

1. customer request details

The combination of the full path and name of the requested page is the unique address of the server when the browser requests the page. The browser request can also contain the address of the browser host and the operating system running on the client. The actual information content varies with the browser, and only a small part of the content can be provided by other applications such as the search engine robot. For a clearer understanding of this information, the following is a page http://www.wrox.com/Store/ issued from IE 5.0

Download. asp Request Information:

7/8/99 10:27:16 Sent GET/Store/Download. asp HTTP/1.1
Accept: application/msword, application/vnd. ms-execl, application/vnd. ms-
Powerpoint, image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-
Comet ,*/*
Accept-Language: en-us
Encoding: gzip, deflate
Referer: http://ww.wrox.com/main_menu.asp
COOKIE: visitcount = 2 & lastdate = 6% 2f4% 2f99+ 10% 3a10% 3a13 + AM
User-Agent: Mozilla/4.0 (compatible; MSIE 5.0; Windows 98)
HOST: 212.250.238.67
Connection: keep-alive

It can be seen that the information contains details about the connection between the user proxy and the user (such as the default language), as well as a list of acceptable file or application types, these are all MIME-type, and more will be seen later. The browser can accept several image files and a variety of Microsoft Office file types. The "standard" file type, such as tesx/html and text/text are not listed in it. */* In the file list indicates that any type of file can be sent back to the browser, which can be interpreted by the browser or by a plug-in application.

Cookie: the cookie contained in the entry is stored on the client computer and only valid for this domain. If the request is a result of clicking a link, rather than directly entering a URL in the address bar of the browser, the Referer: entry is displayed, which contains the complete URL of the link page.

Host: Enter the IP address or name of the customer's computer. However, this is not enough to accurately identify the client. Because the IP addresses are dynamically allocated when they are connected through the ISP, or when they are connected through a proxy server, the IP addresses are the proxy server rather than the actual client.

2. Server Response Details

To respond to the preceding request and provide the request page to an anonymous browser (that is, the user does not need to provide the user name and access password), the following content is sent from the server to the client:

7/8/99 10:27:16 received HTTP/1.1 200 OK
Server: Microsoft-Microsoft IIS/5.0
Connection: keep-alive
Date: Thu, 8 Jul 1999 10:27:16 GMT
Content-Type: text/html
Accept-ranges: bytes
Content-Length: 2946
Last-modified: Thu, 8 Jul 1999 10:27:16 GMT
COOKIE: visitcount = 3 & lastdate = 7% 2f8% 2f99 + 10% 3a27% 3a16 + AM
<HTML>
... Rest of page...
</Html>

The server shows the software and version used by the client. The first line indicates that the HTTP protocol and the return code are used. "200 OK" indicates that the request is accepted and satisfied. The following information is the details of the returned page, including the MIME type (Content-Length :), size (bytes), recent modification time, and returned cookie stored on the client. Other information in the response is the information flow of the page content.

In some cases, the server cannot return a page after responding to a request, maybe because the page does not exist, or the customer does not have the corresponding permissions to access it. We will discuss security issues later. Now, if the request page does not exist (for example, the user entered an incorrect URL in the address bar of the browser), the returned information starts:

7/8/99 14:27:16 received HTTP/1.1 404 not found
Server: Microsoft-Microsoft IIS/5.0
...

The status code and information indicate that the page requested by the customer cannot be found. The browser can use this information to display the corresponding information to the user (in this case, the server response information is not displayed in IE 5.0, but the corresponding "help" error prompt page is displayed ), you can also display the default page created by the server (depending on the settings of the server ). Request and Response objects
The Application of client Request and Server Response Details in ASP is implemented through the ASP built-in Request and Response object.

· Request object: provides all the information provided by the client when the client requests a page or transmits a form. This includes the HTTP variables that can indicate the browser and the user, and the Cookies stored in the browser under this domain name, the value of any HTML control in the <FORM> section of the string or page appended to the URL as the query string. It also provides authorized access using Secure Socket Layer (SSL) or other encrypted communication protocols and attributes that help you manage connections.

· Response object: used to access the Response information created by the server and returned to the client concurrently. Provide HTTP variables for the script, specify the functions of the server and server, information about the content sent back to the browser, and any new cookies that will be stored in the browser for this domain. It also provides a series of methods to create output, such as the ubiquitous Response. Write method.

1. Overview of Request object members

A) set of Request objects

The Request object provides five sets that can be used to access various types of information requested by the client to the Web server. These sets are shown in the following table:

Set and description of Request objects

Set Name Description
Clientcertificate When a client accesses a page or other resources, it is used to indicate the identity of the client certificate to the server. Each member is read-only.
Cookies The set of all cookie values sent by the user system based on the user's request. These cookies are only valid for the corresponding domain, and each member is read-only.
Form When the property value of METHOD is POST, all the values of the HTML control unit in the <FORM> segment submitted as the request are read-only members.
QueryString The name/value pair attached to the URL of the user request or submitted as a request and the METHOD attribute is GET (or its attribute is omitted, or the value of all HTML control units in <FORM>. Each member is read-only.
ServerVariables The HTTP header value sent along with the client request and the set of environment variable values of the Web server. Each member is read-only.

B) attributes of the Request object

The unique attributes and descriptions of the Request object are shown in the following table. It provides the number of bytes requested by the user. It is rarely used on ASP pages. We usually focus on the specified value rather than the entire Request string.

Attribute Description
TotlBytes Read-only, returns the total number of bytes of the request sent by the client

C) Method of the Request object

The unique method and description of the Request object are shown in the following table. It allows access to the complete content of the user Request part passed to the server from a <FORM> segment.

Method and description of the Request object

Method Description
BinaryRead (count) When the data is sent to the server as part of the POST request, the data in the count bytes is obtained from the customer request, and a Variant array (or SafeArray) is returned ). If the ASP code has referenced the Request. Form set, this method cannot be used. If the BinaryRead method is used, the Request. Form set cannot be accessed.

2. Response object member Overview

A) set of Response objects

The Response object has only one set. As shown in the following table, this set sets the cookie value to be placed on the customer system, which is directly equivalent to the Request. Cookies set.

Response object set and description

Set Name Description
Cookies In the current response, all cookie values of the client are returned. This set is write-only.

B) attributes of the Response object

The Response object also provides a series of attributes that can be read (in most cases) and modified so that the Response can adapt to the request. These settings are set by the server. We do not need to set them. Note that when setting certain attributes, the syntax used may be different from what is usually used.

Attributes and descriptions of Response objects

Attribute Description
Buuffer = True | False Read/write, Boolean, indicates whether the output created by an ASP page is stored in the IIS Buffer until all server scripts on the current page are processed or the Flush and End methods are called. This attribute must be set before any output (including HTTP submission information) is sent to IIS. Therefore, in the. asp file, this setting should be in <% @ LANGUAGE =... %> The first line after the statement. ASP 3.0 sets buffer to True by default, and False by default in earlier versions)
CacheControl "setting" Read/write, writable. Set this attribute to "Public" to allow the proxy server to cache pages. If it is "Private", the proxy server cache is prohibited.
Charset = "value" Read/write, character Type, attaches the character set name used (for example, ISO-LATIN-7) to the HTTP Content-Type Header created by the server for each response)
Content Type = "MIME-type" Read/write, response type, indicating the HTTP content type of the response, standard MIME type (such as "text/XML" or "Image/gif "). By default, the MIME type "text/html" is used. The content type tells the browser of the expected content type.
Expires minutes Read/write, numeric type, indicating the duration of the page effective in minutes. If the user requests the same page before its validity period expires, the content in the buffer will be directly read, after this period of validity, the page will not be retained in the private (User) or public (proxy server) buffer.
Expires Absolute # date [time] # Read/write, date/time type, indicating the absolute date and time when a page expires and is no longer valid
IsClientConnected Read-only and boolean type. It indicates whether the client is still connected to or downloading the page. Before the current page is executed, if a customer moves to another page, this flag can be used to stop processing (using the Response. End method)
PICS ("PICS-Label-string ") Write-only and compile-type: Create a PICS header to define the vocabulary level in the page content, such as brute force, sex, and poor language.
Status = "Code message" Read/write, response type, indicating the status value and information indicating whether the error or page processing is successful in the HTTP header of the response sent back to the customer. For example, "200 OK" and "404 Not Found"

C) Method of the Response object

The Response object provides a series of methods, as shown in the following table, allowing you to directly process the page content created for return to the client.

Response object method and description

Method Description
AddHeader ("name", "content ") Create a custom HTTP header by using the name and Content values and add it to the response. The existing headers with the same name cannot be replaced. Once a header is added, it cannot be deleted. This method must be used before any page content (text and HTML) is sent to the client.
AppendToLog ("string ") When the W3C Extended Log File Format is used, add an entry to the Log File of the Web server requested by the user. At least select "URL Stem" on the "Extended Properties" page of the site that contains the page"
BinaryWrite (safeArray) Write the Variant type SafeArray in the current HTTP output stream without any character conversion. It is very useful for writing non-string information, such as binary data requested by a custom application or binary bytes that constitute an image file.
Clear () When Response. Buffer is True, the existing cached page content is deleted from the IIS Response Buffer. However, if you do not delete the HTTP Response Header, You can discard partially completed pages.
End () Let ASP end the page processing script, return the currently created content, and then discard any further processing of the page.
Flush () Send all the current buffer pages in the IIS buffer to the client. When Response. Buffer is True, it can be used to send part of the content of a large page to individual users.
Redirect ("url ") By sending a 302 Object Moved HTTP header in the response, instruct the browser to download the page of the corresponding address based on the string url
Write ("string ") Write specified characters in the current HTTP response information stream and IIS Buffer to make it part of the returned page

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.