ASP's request and response object use detailed

Source: Internet
Author: User
Tags character set flush html page iis log relative domain
Both the request and response objects are the most commonly used two of the built-in objects provided by the ASP. Between browsers (or other user agents) and Web servers, the exchange of information that occurs in requests and responses can be accessed and managed through two built-in objects in the ASP, called the request and response objects.

Almost all of the work you want to do in an ASP page is to access both objects, and the way you use them affects the efficiency and reliability of your pages. Their main purpose, of course, is to access the values that users send back to the server, either from the <FORM> segment of the HTML page or as a query string after the URL, and to create the appropriate output to return to the user, and they can share many of the same factors. For example, two objects can use cookies that are stored on the client computer.

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

The contents of the study are:

· How the client communicates with the server to deliver the Web or other resources.

· The details of the request and response objects, and the similarities between them.

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

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

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

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

Communication between the client and the server

To save space, the term "browser" (browser) is used in the following context. But keep in mind that applications that can access Web pages are not just browsers, there are many special applications that download Web pages from sites, such as special client programs designed for visually impaired users or other difficulties with the usual browsers. The most obvious example is the robot that search engines use to access sites on the web. Consider these factors comprehensively, including a common web browser, and the exact words should be the user agent.

Dialog for page requests

When a browser presents a page request to a Web site, it is clear that the server must be told which page it is requesting. The first thing to do is to establish a connection with the server through the domain name, and then provide the full path and name of the requested page. Why do I need full path and name? The web is a borderless environment, so you must create a session to identify each customer (how the ASP will do this later).

This means that every time the server completes sending a page to the customer, the server completely forgets about the customer. Therefore, when the customer requests the next page, it is exactly the same as a new visitor. The server is unable to remember the customer and, accordingly, cannot determine which page they last requested. Because you cannot use a relative path 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 current page's domain and path, or by using the <BASE> element in the page <HEAD> section, telling the browser what the URL is for all links in a page. For example:

<base herf=http://www.wrox.com/store>

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

1, the customer request details

A combination of the full path and name of the requested page is the only place where the browser requests a page to be sent to the server. The browser's request can also contain the domicile of the browser host and the operating system that the client is running. The actual content of the information will change with the browser, and only a few can be provided by other applications such as search engine robot. To get a clearer picture of this information, here is a page http://www.wrox.com/Store/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

As you can see, the information contains details about the user agent and user connections (such as the default language), as well as a list of the types of files or applications that can be accepted, all of which are MIME types and will be met later. Browsers can accept several image files and a variety of Microsoft Office file types. "Standard" file types, such as tesx/html and Text/text, are not listed therein. The */* in the file list indicates that any type of file can be sent back to the browser, interpreted by the browser, or explained by a plug-in (plug-in) application.

Cookie: The entry contains a cookie that is stored on the client computer and is valid only for that domain. If the request is the result of clicking on the link instead of entering the URL directly in the address bar of the browser, Referer: The entry is displayed and contains the full URL of the linked page.

Host: Entry contains the IP address or name of the client computer. However, this is not sufficient to accurately identify the client. Because the IP address is dynamically assigned when they are connected through the ISP, or when connected through a proxy server, the IP address is the agent rather than the actual client.

2, the server response details

To respond to the above request, and to provide the requested page for the Anonymous browser (that is, the user does not have to provide a username and access password), the following is sent from the server to the client:

7/8/99 10:27:16 Received http/1.1 OK
server:microsoft-iis/5.0
Connection:keep-alive
Date:thu, 8 June 1999 10:27:16 GMT
Content-type:text/html
Accept-ranges:bytes
content-length:2946
Last-modified:thu, 8 June 1999 10:27:16 GMT
Cookie:visitcount=3&lastdate=7%2f8%2f99+10%3a27%3a16+am
<HTML>
... rest of page ...
</HTML>

You can see the server to the client to explain their own software and version, the first line indicates that the HTTP protocol used, and return code status. Information "OK" indicates that the request was accepted and satisfied. The following information is the details of the returned page, including the MIME type (content-length:), size (bytes), last changed time, and the cookie that returns the client store. Other information in the response is the flow of content in the page.

In some cases, the server cannot return a page after responding to a request, perhaps because the page does not exist or the client does not have the appropriate permissions to access it. We'll discuss security issues later. Now, for situations where the request page does not exist (for example, the user has entered the wrong URL in the browser's address bar), the information returned begins with:

7/8/99 14:27:16 Received http/1.1 404 Not Found
server:microsoft-iis/5.0
...
Here, the status code and information indicate that the page requested by the customer cannot be found. The browser can use this information to display to the user the appropriate information (this situation in IE 5.0 does not display the server's response information, and display the appropriate "Help" Error prompt page), you can also display the server created default page (depending on the server settings). Request and Response objects
The details of the ability to apply customer requests and server responses from an ASP are implemented through the ASP's built-in request and response objects.

· Request object: Provides the script with all the information the client provides when the client requests a page or passes a form. This includes an HTTP variable that indicates the browser and the user, a cookie stored in the browser under the domain name, and any value of the HTML control in the string or the <FORM> segment of the page that is appended to the URL as a query string. Also provides authorization access using secure Socket Layer (SSL) or other cryptographic communication protocols, and properties that help manage the connection.

· Response object: Used to access the response information created on the server side of the concurrency back to the client. Provides the HTTP variables for the script, indicating the capabilities of the server and server and information about what to send 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 for creating output, such as the ubiquitous Response.Write method.

1, the Request object member's summary

A) collection of request objects

The Request object provides 5 collections that you can use to access the various types of information that the client requests from the Web server, which are set out in the following table:

Collection and description of the request object

collection name description
clientcertificate
cookies According to the user's request, A collection of the values of all cookies emitted by the user system that are valid only for the corresponding domain, each member being read-only
method property value is post, all collections of values for the HTML control cells in the segment that are submitted as requested, each member is read-only
quer ystring , each member is read-only
servervariables HTTP header values issued with the client request, and a collection of the values of several environment variables for the Web server, each member is read-only

b) The properties of the Request object

The request object's unique properties and description, which provides information about the number of bytes requested by the user, is rarely used for ASP pages, and we usually focus on the specified value rather than the entire request string.

Property Description
Totlbytes Read-only to return the entire number of bytes requested by the client

c) The method of the Request object

The only method of the request object and its description, as shown in the following table, allows access to the complete contents of the user Request section that is passed to the server from a <FORM> segment.

Method and description of the request object

Method Description
BinaryRead (count) When data is sent to the server as part of a POST request, the count byte data is obtained from the customer request, and a Variant array (or SAFEARRAY) is returned. This method is not available if the ASP code already references the Request.Form collection. Also, if you use the BinaryRead method, you cannot access the Request.Form collection

2, Response Object member overview

A) A collection of response objects

The response object has only one collection, as shown in the following table, which sets the value of the cookie that you want to place on the client system, which is directly equivalent to the Request.Cookies collection.

Collection and description of response objects

Collection name Description
Cookies In the current response, the value of all cookies that are sent back to the client, and this collection is write-only

b) Response properties of the object

The response object also provides a series of properties that can be read (in most cases) and modified so that the response adapts to the request. These are set by the server and we do not need to set them. It is important to note that when certain properties are set, the syntax used may differ somewhat from what is commonly used.

Properties and descriptions of response objects

Property Description
buuffer=true| False Read/write, Boolean, indicating whether the output created by an ASP page is kept in the IIS buffer until all server scripts for the current page have been processed or the flush, end method is invoked. This property must be set before any output, including HTTP submission information, is sent to IIS. Therefore, in the. asp file, this setting should be the first line following the <% @LANGUAGE =...%> statement. The ASP 3.0 default is buffered to open (True) and the default is off in earlier versions (FALSE)
CacheControl "Setting" Read/write, character type, set this property to "public" allows the proxy server to cache pages, such as "Private" to prevent proxy server caching from occurring
charset= "Value" Read/write, character type, with the name of the character set used in the HTTP Content-type header created by the server for each response (for example: iso-latin-7)
Content type= "Mime-type" Read/write, character type, indicating the HTTP content type of the response, the standard MIME type (such as "Text/xml" or "image/gif"). If the default indicates that the MIME type "text/html" is used, the content type tells the browser what type of content is expected
Expires minutes Read/write, numeric, indicating the length of time the page is valid in minutes, and if the user requests the same page before its full validity period, the contents of the display buffer will be read directly, after which the page will no longer remain 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, Boolean, return whether the customer still connects and downloads the status flag of the page. This flag can be used to abort processing (using the Response.End method) if a customer moves to another page before the current page has finished executing.
PICS ("pics-label-string") Write-only, character-type, create a pics header to define the lexical level in the page content, such as violence, sex, bad language, etc.
status= "Code Message" Read/write, character type, indicating the status value and information that indicates whether the error or page processing succeeded in the HTTP header of the response sent back to the customer. For example, "OK" and "404 Not Found"

c) Methods of response objects

The Response object provides a series of methods, as shown in the following table, that allow direct processing of page content created for return to the client.

Method and description of response object

Method Description
AddHeader ("name", "content") By using the name and content values, a custom HTTP header is created and added to the response. The existing header of the same name cannot be replaced. Once a header has been added, it cannot be deleted. This method must be used before any page content (i.e. text and HTML) is sent to the client
AppendToLog ("string") When using the "Extended log file format" file format, add an entry for the log file of the Web server that the user requests. At a minimum, select URL Stem on the Extended Properties page of the site that contains the page
BinaryWrite (SafeArray) Writes a variant type of SAFEARRAY to the current HTTP output stream without any character conversions. This is useful for writing information that is not string, such as binary data requested by a custom application or binary byte that makes up an image file
Clear () Deletes the existing buffered page content from the IIS response buffer when Response.Buffer is true. The header of the HTTP response is not removed, and can be used to discard partially completed pages
End () Let ASP end the script for processing the page and return what is currently created, and then discard any further processing of the page
Flush () Sends all current buffer pages in the IIS buffer to the client. When Response.Buffer is true, it can be used to send portions of a larger page to individual users
Redirect ("url") Instructs the browser to download a page of the corresponding address based on the string URL by sending a "302 Object moved" HTTP header in the response
Write ("string") Writes the specified character to the current HTTP response information flow and the IIS buffer, making it part of the return 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.