Sun Chili!Soft ASP Sun Chili!Soft
ASP Sun Microsystems

 

ASP Response Object AddHeader Method

The AddHeader method adds an HTML header with a specified value. This method always adds a new HTTP header to the response. It will not replace an existing header of the same name. Once a header has been added, it cannot be removed.

This method is for advanced use only. If another Response method will provide the functionality you require, it is recommended that you use that method instead.

Syntax: ASP Response Object AddHeader Method

Response.AddHeader name, value

Parameters: ASP Response Object AddHeader Method

name

The name of the header variable.

value

The value assigned to the header variable.

Remarks: ASP Response Object AddHeader Method

To avoid any name ambiguity, the name of the header should not contain any underscores (_). The Request.ServerVariables collection interprets underscores as dashes in the header name. The following script causes a search for a header named "My-Header":

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

Because HTTP protocol requires that all headers be sent before content, you must call the AddHeader method in your script before any output (such as that generated by HTML code or the ASP Response Object Write Method) is sent to the client. The exception to this rule is when the ASP Response Object Buffer Property is set to True. If the output is buffered, you can call the AddHeader method at any point in the script, as long as it precedes any calls to the ASP Response Object Flush Method. Otherwise, the call to AddHeader will generate a run-time error.

The following two examples illustrate this. In the first example, the page is not buffered. The script works, however, because the AddHeader method is called before the server sends the Web page to the client. If the order were reversed, the call to the AddHeader method would generate a run-time error.

<% Response.AddHeader "WARNING", "Error Message Text" %>

<HTML>

Some text on the Web page.

</HTML>

In the next example, the page is buffered, and as a result, the server will not send output to the client until all the ASP scripts on the page have been processed or until the Flush method is called. With buffered output, calls to the AddHeader method can appear anywhere the script, so long as they precede any calls to the Flush method. If the call to the AddHeader method appeared below the call to the Flush method in the preceding example, the script would generate a run-time error.

<% Response.Buffer = TRUE %>

' Here is some text on your Web page.

<% Response.AddHeader "WARNING", "Error Message Text" %> Here's some more interesting and illuminating text.

<% Response.Flush %>

<%= Response.Write("some string") %>

Examples: ASP Response Object AddHeader Method

The following example uses the AddHeader method to request that the client use BASIC authentication.

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

Note

The preceding script merely informs the client browser which authentication to use. If you use this script in your Web applications, you should ensure that the Web server has BASIC authentication enabled.

Copyright 2002 Sun Microsystems, Inc. All rights reserved. Legal Notice.