Analysis of HTTP header operation method in ASP

Source: Internet
Author: User
We have seen in several places how ASPs Create or modify the HTTP headers that are sent to the customer in response to a page request. There are several properties and methods in the response object that can help us do a little. Here are some header methods:

· Controls caching and expiration.

· Creates a state and a custom HTTP header.

· Specifies the MIME type or content type.

· Add a Pics label.

The next step is to study each aspect briefly. On the Response Object home page (show_response.asp), click the related property name or method name to check the properties and methods that we are talking about.

1. Cache and "Expiration" ASP Web pages

Users ' browsers, as well as their any proxy servers with servers, can cache HTML and Web pages created with ASP. When the user subsequently requests the page, the browser sends a "last modified" request to the server (using a http_if_modified_since header that contains the date of the cached version) to ask if the page has been modified.

If it is not modified, the server applies the status code and the message "304 not Modified" to respond, the browser will use the cached content instead of downloading a copy over the network. If a modified version already exists, it is sent with the "OK" status code and the message.

1) Response.cachecontol Property

Other factors can also affect the process. However, any proxy server within the network route used by the Web page (typically located on the client side) can be discarded by setting the Response.CacheControl property as private to discard the cached Web page. This is the default for ASP Web pages in ASP 3.0 and is not set. This is especially useful when web pages are specially tailored for individual visitors. This can prevent other users on the same network from entering the same Web page. Allows the server to cache Web pages when the CacheControl property value is set to public. Note that some proxy servers may behave differently, or ignore or bypass the header.

In IE4, it is possible to get a false "this page has expired" message when the proxy server cache is available. We have provided a Web page (expiretest_form.asp) that can be tested on the network through its own proxy server to check the impact of this property. You can click Response in the Response Object home page. CacheControl "link to display this page. As shown in the following illustration:

When this page is submitted to the Expiretest_result.asp Web page, you can set the Response.CacheControl property, and then insert the value in the Web page and the time the script was executed:

<%
If Request.Form ("public") = "on" Then ' cache-control check box is ticked
Response.CacheControl = "Public"
Else
Response.CacheControl = "Private"
End If
%>
<HTML>
...
Cache-control is: <B> <% = Response.CacheControl%> </B> <p>
Value in text box is: <B> <% Response.Write Request.Form ("textbox")%>
<%
Response.Write Right ("0" & Hour (now), 2) & ":" & Right ("0" & Minute (now), _
& 2) & ":" & Right ("0" & Second (now), 2)
%> </B>

You can see whether the code executes automatically or uses a cached copy by clicking Back and Forward on the browser.

2) Response.Expires and Response.ExpiresAbsolute properties

The two properties that control the cached Web page storage time are the expires and Expriesabsolute properties of the response object. Response.Expires defines the length of time that a wind page should remain valid before it is discarded from the buffer, in minutes that have been created. The ExpiresAbsolute property sets an absolute date and time for the expiration time.

We provide an example Web page named Addheaders_form.asp that demonstrates how to use these properties. Click the link to the two properties in the Response Object home page.

In the resulting page, you can add your own custom HTTP headers and set several properties that affect the HTTP headers of the response. When you click on the Submit Query Content button, page show_headers.asp adds the selected header to the returned data stream, and then displays the code that is used to complete the operation, showing the appropriate execution time, which can be used to check whether the page is cached or executed again.

Show_headers.asp the code in the Web page creates and adds an HTTP header, as follows:

<%
' Write HTTP headers before any other output
If Request.Form ("expires") = "On" Then _
Response.Expires = Request.Form ("Expires_value")
If Request.Form ("expiresabs") = "On" Then _
Response.ExpiresAbsolute = Request.Form ("Expiresabs_value")
If Request.Form ("lastmod") = "On" Then _
Response.AddHeader "Last-modified", Cstr (Request.Form ("Lastmod_value"))
If Request.Form ("pragma") = "On" Then _
Response.AddHeader "PRAGMA", CStr (Request.Form ("Pragma_value"))
If Request.Form ("refresh") = "On" Then _
Response.AddHeader "REFRESH", CStr (Request.Form ("Refresh_value"))
If Request.Form ("addheader") = "on" and Len (Request.Form ("Addheader_name")) Then _
Response.AddHeader CStr (Request.Form ("Addheader_name")), _
CSTR (Request.Form ("Addheader_value"))
If Request.Form ("status") = "On" Then _
Response.Status = Request.Form ("Status_value")
%>
<HTML>
...
... Show code and execution time
...

The remainder shows only the code that has been executed and the execution time. Readers will notice the custom header "PRAGMA" included in the Web page (which we haven't discussed yet). Some (previous) proxy servers use it as an indication of whether the net magnetism should be cached. The default is that the Web page is buffered unless the HTTP header "Pragma=no-cache" is accepted.

2. Create a status code and a custom HTTP header

You can use the AddHeader method of the response object that you saw earlier in the instance Web page to create your own status code or your favorite custom header. This method requires two parameters: an HTTP header name or a string containing its value or the value assigned to it. As an example, the following code adds a refresh header to the page:

Response.AddHeader "REFRESH", "60; Url=newpath/newpage.asp "

This equates to the client side <META> element:

<meta http-equiv= "REFRESH", "60; Url=newpath/newpage.asp ">

In other words, you can also use the AddHeader method with the Status property to load a browser into a new page:

Response.Status = "302 Object moved"
Response.AddHeader "Location", "newpath/newpage.asp"

This equates to using the Response.Redirect method:

Response.Redirect "Newpath/newpage.asp"

The Response.Status property can be used to send some required status messages, such as adding the following lines:

Response.status= "401 Unauthorized"

Response.AddHeader "Www-authenticate", "BASIC"

Force the browser to display a username/password dialog box, and then use Basic authentication to send them back to the server (the validation method will be seen later in this series).
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.