Freshers Aptitude technical questions
Freshers Job Alert
Bookmark and Share

  Expires, Expires Absolute, AddHeader

 

The Expires property specifies the length of time before a page cached on a browser expires. If the user returns to the same page before it expires, the cached version is displayed.

Syntax

Response.Expires [ = number ]

Parameters

number

The time in minutes before the page expires.

Remarks

When your .asp file calls Response.Expires , IIS creates an HTTP header indicating the time on the server. If the system time on the client is earlier than the system time on the server (due to either the client or server having an inaccurate time setting, or time-zone differences) setting the parameter to 0 will not have the effect of expiring the page immediately. You can use the Response.ExpiresAbsolute property to achieve immediate expiration of a page. In addition, you can use a negative number for the Expires property. For example

<%Response.Expires = -1 %>

will expire the response immediately.

If there are multiple calls to Response.Expires on a single page, the server will use the shortest time period.

ExpiresAbsolute

The ExpiresAbsolute property specifies the date and time at which a page cached on a browser expires. If the user returns to the same page before that date and time, the cached version is displayed. If a time is not specified, the page expires at midnight of that day. If a date is not specified, the page expires at the given time on the day that the script is run.

Syntax

Response.ExpiresAbsolute [ = [ date ] [ time ]]

Parameters

date

Specifies the date on which the page will expire. The value sent in the expires header conforms to the RFC-1123 date format.

time

Specifies the time at which the page will expire. This value is converted to GMT before an Expires header is sent.

Remarks

If this property is set more than once on a page, the earliest expiration date or time is used.

Example

The following example specifies that the page will expire 15 seconds after 1:30 PM on May 31, 2001 .

<% Response.ExpiresAbsolute=# May 31,2001 13:30:15 # %>

 

CacheControl

The CacheControl property allows you to set the HTTP/1.1 Cache-Control header in a response.Syntax

Response.CacheControl [ = Cache Control Header ]

Parameters

Cache Control Header

The following is a partial list of values supported by the HTTP/1.1 Protocol. Please see the Hypertext Transfer Protocol -- HTTP/1.1 specification at the World Wide Web Consortium Web site , section 14.9, for more complete descriptions.

Value

Description

Private

A cache mechanism may cache this page in a Private cache and resend it only to a single client. This is the default value. Most proxy servers will not cache pages with this setting.

Public

Shared caches, such as proxy servers, will cache pages with this setting. The cached page can be sent to any user.

No-cache

Do not cache this page at all, even if for use by the same client.

No-store

The response and the request that created it must not be stored on any cache, whether shared or private. The storage inferred here is non-volatile storage, such as tape backups. This is not an infallible security measure.

Note

Between your Web server and a user requesting your page, there may be proxy servers configured to cache Web pages for faster response times. Usually ASP pages are developed to be unique for each user, or may contain secure information. For this reason, IIS sets this property to "Private" so that proxy servers or other cache mechanisms will not cache pages. You can override this default value, setting it to any value supported by the HTTP/1.1 protocol, documented in the Hypertext Transfer Protocol -- HTTP/1.1 specification at the World Wide Web Consortium Web site .

If there is no cache mechanism between your Web server and a client computer, or if a proxy server is running HTTP/1.0, CacheControl will be ignored.

Setting CacheControl to "public" may seem to improve the performance of your .asp files, but it is discouraged if you generate custom HTML for every request, and a proxy server might interfere with the response.

The values for CacheControl are strings, and must be enclosed in quotation marks (" "). You must set CacheControl before any response is sent to the client unless response buffering is enabled.

Example

CacheControl comes before the <HTML> tag to ensure that it is set before content is sent to the client.

--- CacheControl_NoBuffer.asp ---

<% Response.Buffer = False Response.CacheControl = "private" %> <HTML><HEAD><TITLE>Response.CacheControl Example</TITLE></HEAD> <BODY> Output from this page is sent to the client as it is being processed.<BR> It will not be cached.<BR> Today is <%= Date %>, <%= Time %><BR> <H3>Please enter your credit card number:</H3> <FORM NAME="Order" METHOD="POST" ACTION="order.asp "> <INPUT TYPE="TEXT" NAME="CreditCard"> <INPUT TYPE="SUBMIT" VALUE="Submit" NAME="Submit"> </FORM> </BODY> </HTML>

--- CacheControl_Buffer.asp ---

<% Response.Buffer = True %> <HTML><HEAD><TITLE>Response.CacheControl Example</TITLE></HEAD> <BODY> Output from this page is sent to the client once it is completely processed by the server, so we can set CacheControl anytime.<BR> <% Response.CacheControl = "private" %> It will not be cached.<BR> Today is <%= Date %>, <%= Time %><BR> <H3>Please enter your credit card number:</H3> <FORM NAME="Order" METHOD="POST" ACTION="order.asp "> <INPUT TYPE="TEXT" NAME="CreditCard"> <INPUT TYPE="SUBMIT" VALUE="Submit" NAME="Submit"> </FORM> </BODY>

</HTML>

AddHeader

The AddHeader method adds a new HTML header and value to the response sent to the client. It does not replace an existing header of the same name. Once a header has been added, it cannot be removed.

Syntax

Response.AddHeader HeaderName , HeaderValue

Parameters

HeaderName

A string that indicates the name of the new header.

HeaderValue

A string that indicates the initial value of the new header.

Note

If a client is configured to return response headers back to the server on a subsequent request, you may use Request.ServerVariables to retrieve the custom header value. "HTTP_" will be pre-pended to the custom header name. For example, if you add a header with this code:

<% Response.AddHeader "CustomHeader", "CustomValue" %>

You can retrieve the header if a special client returns it to the server on the next request with this code:

<% ReturnedValue = Request.ServerVariables("HTTP_CustomHeader") %>

To avoid name ambiguity, HeaderName should not contain any underscore (_) characters. The ServerVariables collection interprets underscores as dashes in the header name. For example, the following script causes the server to search for a header named MY-HEADER.

<% Request.ServerVariables("HTTP_MY_HEADER") %>

If another Response method can provide the functionality you require, it is recommended that you use that method instead. For example, to send a custom value and have it returned to your Web application on a subsequent request without relying on the configuration of the client, you can use Response.Cookies and Request.Cookies ; or to set cache control for a response, use Response.CacheControl .

Because the HTTP protocol requires that all headers be sent before content, you must modify all outgoing headers before your ASP script generates any output. In IIS 4.0, this meant that you had to call AddHeader in your script before any output was sent to the client, such as output generated by HTML code or the Write method. In IIS versions 5.0 or later, response buffering is on by default. Therefore, you can call the AddHeader method at any point in the script, as long as it precedes any calls to Flush . You can enable or disable response buffering by setting the metabase property AspBufferingOn or making a call to Response.Buffer in an ASP script.

The following .asp file illustrates this point.

<HTML> Here's some text on your Web page. ' This header tells proxy servers using HTTP/1.0 not to cache this request. <% Response.AddHeader "Pragma", "no-cache" %> <% Response.Flush %> <% Response.Write("Pragma is set to no-cache") %> </HTML>

In the preceding example, because the page is buffered by default, the server will not send output to the client until all the scripts on the ASP page have been processed or until the Flush method is called. If the call to AddHeader appeared after the call to Flush in the preceding example, the script would generate a run-time error.

Example

You can use AddHeader to send multiple copies of the same header with different values, as with the WWW-Authenticate headers. The following example uses the AddHeader method to request that the client use Basic authentication.

<% Response.Addheader "WWW-Authenticate", "BASIC" %>

The preceding script only informs the client browser which type of authentication to use; it will not tell your Web server to enable Basic authentication for the application.