//  home   //  advanced search   //  news   //  categories   //  sql build chart   //  downloads   //  statistics
 ASP FAQ 
Home
ASP FAQ Tutorials

   8000XXXX Errors
   Alerts
   ASP.NET 2.0
   Classic ASP 1.0
      COM / ActiveX Components
      Forms
      General Topics
      Date/Time Routines
      Email Scripts & Info
      Files/Directories & FSO
   Databases
   General Concepts
   Search Engine Optimization (SEO)

Contact Us
Site Map

Search

Web
aspfaq.com
tutorials.aspfaq.com
classicasp.aspfaq.com

ASP FAQ Tutorials :: Classic ASP 1.0 :: General Topics :: What do I need to know about Response.Redirect?


What do I need to know about Response.Redirect?


Basics of Response.Redirect
    When you request a page from a web server, the response you get has some headers at the top, followed by the body of the page. When viewed in your browser the headers are never seen, but are used by the browser application. I have the following page called test.asp; 
     
    <HTML> 
    <HEAD> 
    <META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0"> 
    </HEAD> 
    <BODY> 
     
    <p>Hello</p> 
     
    </BODY> 
    </HTML>
     
    When I request that from the web server this is the reply I get; 
     
    HTTP/1.1 200 OK 
    Server: Microsoft-IIS/5.0 
    Date: Mon, 19 Mar 2001 15:07:44 GMT 
    Connection: close 
    Content-Length: 134 
    Content-Type: text/html 
    Set-Cookie: ASPSESSIONIDQQGQQJWO=OMCJFABDNCDLLBKAPNHJBKHD; path=/ 
    Cache-control: private 
     
    <HTML> 
    <HEAD> 
    <META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0"> 
    </HEAD> 
    <BODY> 
     
    <p>Hello</p> 
     
    </BODY> 
    </HTML>
     
    The first line returns the status of the response, in this case "200 OK" which means everything is fine. The following lines are headers, these are in the format of 
     
    Name: Content 
     
    So in our example the web server is identifying itself as Microsoft-IIS/5.0, it also sends its date and time, the content type and it also instructs the browser to store a cookie. This cookie contains your session ID and is used by IIS to remember who you are. After the headers there is a blank line then the actual HTML to be shown in the browser. 
     
    If I request a page that does not exist then we get this back; 
     
    HTTP/1.1 404 Object Not Found 
    Server: Microsoft-IIS/5.0 
    Date: Mon, 19 Mar 2001 15:11:54 GMT 
    Connection: close 
    Content-Type: text/html 
    Content-Length: 3243 
     
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"> 
    <html dir=ltr> 
     
    <head> 
    <style> 
    a:link {font:8pt/11pt verdana; color:FF0000} 
    a:visited {font:8pt/11pt verdana; color:#4e4e4e} 
    </style> 
     
    <META NAME="ROBOTS" CONTENT="NOINDEX"> 
     
    <title>The page cannot be found</title> 
    ...
     
    As you can see, the status is now "404 Object Not Found" and the HTML is generated for us by the web server. So the web server uses different response status types to inform the browser the nature of the response itself. Let's modify our test.asp code to read this; 
     
    <% 
        response.redirect "test2.asp" 
    %> 
     
    <HTML> 
    <HEAD> 
    <META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0"> 
    </HEAD> 
    <BODY> 
     
    <p>Hello</p> 
     
    </BODY> 
    </HTML>
     
    Note the code at the top of the page that does a "response.redirect". If we request this page we get the following; 
     
    HTTP/1.1 302 Object moved 
    Server: Microsoft-IIS/5.0 
    Date: Mon, 19 Mar 2001 15:16:35 GMT 
    Connection: close 
    Location: test2.asp 
    Content-Length: 130 
    Content-Type: text/html 
    Set-Cookie: ASPSESSIONIDQQGQQJWO=ANCJFABDFLBLHMKIJOIOKJDM; path=/ 
    Cache-control: private 
     
    <head><title>Object moved</title></head> 
    <body><h1>Object Moved</h1>This object may be found <a HREF="test2.asp">here</a>.</body>
     
    The status is "302 Object moved" and note that the actual HTML following the Response.Redirect is not sent to the client. After all, if the page is going to be redirected why send any content? When Internet Explorer gets this type of response it gets the file to be directed to via the Location header 
     
    Location: test2.asp 
     
    And issues another request to get test2.asp. IIS does something clever for us here as well; just in case your browser does not understand 302 headers it generates HTML giving the user a link that they can manually click on. If you are going through a firewall you may have actually seen this code sometimes in your browser. However Internet Explorer does understand 302 headers so shows no page, but simply requests the new page instead. 
     
    That is the simple mechanism where by the web server responds to your requests for pages, however IIS gives us two delivery mechanisms; buffered or non-buffered. When buffering is off IIS sends HTML to the client as it is generated. With buffering on, IIS holds all HTML in a buffer until the page has finished processing, it then hands the HTML to the client in one big batch. 
     
    Try this code; 
     
    <% Response.Buffer = true %> 
    <HTML> 
    <HEAD> 
    <META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0"> 
    </HEAD> 
    <BODY> 
     
    <% 
    for i = 1 to 10000 
        Response.Write i & "<br>" & vbCRLF 
    next 
    %> 
    </BODY> 
    </HTML>
     
    Notice that we are programmatically setting the buffering mode to True, i.e. we want the page buffered. When you navigate to this page the browser will sit and wait, and all of a sudden you'll see 10000 lines in your browser. If you update the code to turn buffering off; 
     
    <% Response.Buffer = false %>
     
    Then you will see the page gradually grow in size until all 10000 lines have been written. Note that IIS4 and IIS5 have different default options for buffering. In IIS4 it is off by default, and in IIS5 it is on by default. This means that if you omit the "Response.Buffer =" code then IIS4 will default to false, and IIS5 to true. 
     
    Let's look at how buffering and redirection can come into conflict. In the following sections I'll explicitly turn buffering on or off so that the code works the same under IIS4 and IIS5. 
     
    Update test.asp to read; 
     
    <% Response.Buffer = false %> 
    <HTML> 
    <HEAD> 
    <META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0"> 
    </HEAD> 
    <BODY> 
     
    <p>Hello</p> 
     
    <% 
        Response.Redirect "test2.asp" 
    %> 
     
    </BODY> 
    </HTML>
     
    The response.redirect is now mid-way through the page. If we view this page we see the following in the browser; 
     
    Hello 
     
    Response object error 'ASP 0156 : 80004005'  
     
    Header Error  
     
    /Examples/test.asp, line 12  
     
    The HTTP headers are already written to the client browser. Any HTTP header modifications must be made before writing page content.
     
    Why do we get this error? Remember that a normal page is sent with "200 OK" in its response headers, and a redirect is "302 Object moved". When IIS hits the first piece of HTML output; 
     
    <HTML>
     
    it deduces that this is a standard "200 OK" page and that the browser should be sent output as it is generated. So it sends this; 
     
    HTTP/1.1 200 OK 
    Server: Microsoft-IIS/5.0 
    Date: Mon, 19 Mar 2001 15:07:44 GMT 
    Connection: close 
    Content-Length: 134 
    Content-Type: text/html 
    Set-Cookie: ASPSESSIONIDQQGQQJWO=OMCJFABDNCDLLBKAPNHJBKHD; path=/ 
    Cache-control: private 
     
    <HTML>
     
    As more HTML is generated it is also sent to the browser to be displayed. When it hits the response.redirect command it has a problem. In order to re-direct it needs to send a "302 Object moved" header but it can't as it has already sent a "200 OK" header, thus the error about not being able to modify the headers. 
     
    Now change the code so that buffering is on; 
     
    <% Response.Buffer = true %> 
    <HTML> 
    <HEAD> 
    <META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0"> 
    </HEAD> 
    <BODY> 
     
    <p>Hello</p> 
     
    <% response.redirect "test2.asp" %> 
     
    </BODY> 
    </HTML>
     
    And try again, this time it works. With buffering on, IIS stores all output in a buffer until the page has completed. So by the time it gets to the redirect it will have this in its buffer; 
     
    HTTP/1.1 200 OK 
    Server: Microsoft-IIS/5.0 
    Date: Mon, 19 Mar 2001 15:07:44 GMT 
    Connection: close 
    Content-Length: 134 
    Content-Type: text/html 
    Set-Cookie: ASPSESSIONIDQQGQQJWO=OMCJFABDNCDLLBKAPNHJBKHD; path=/ 
    Cache-control: private 
     
    <HTML> 
    <HEAD> 
    <META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0"> 
    </HEAD> 
    <BODY> 
     
    <p>Hello</p>
     
    However it has not sent anything to the client yet. So when it hits your redirect it simply throws away anything in it's buffer and replaces it with this instead; 
     
    HTTP/1.1 302 Object moved 
    Server: Microsoft-IIS/5.0 
    Date: Mon, 19 Mar 2001 15:16:35 GMT 
    Connection: close 
    Location: test2.asp 
    Content-Length: 130 
    Content-Type: text/html 
    Set-Cookie: ASPSESSIONIDQQGQQJWO=ANCJFABDFLBLHMKIJOIOKJDM; path=/ 
    Cache-control: private 
     
    <head><title>Object moved</title></head> 
    <body><h1>Object Moved</h1>This object may be found <a HREF="test2.asp">here</a>.</body>
     
    That is sent to the client and the browser can be redirected. 
     
    So to avoid the error when doing a redirect, either put the redirect before any output, or turn on buffering. Remember that if you are using IIS4 you have to turn on buffering programmatically, if you use IIS5 it is done by default. However if you rely on buffering being turned on you should always explicitly add it to your code. That way you won't get problems when you move to a different web server or when Microsoft decide to change the default again. You can also configure buffering to be on or off by default via the IIS MMC.

Why can't I...
    ...redirect to a frame? 
     
    HTTP is a request/response technology. The browser requests a page from the server, and the browser displays the response. For a page to appear in a frame, it is that frame that must make the request. You can't make a request in one frame and have the response piped to another. Only the requester can get the response. 
     
    To work around this you should create HTML that does the frame manipulation on the client; 
     
    <html> 
    <body> 
     
    <p>My page is here</p> 
     
    <script> 
    parent.targetframename.location.replace('page2.asp'); 
    </script> 
     
    </body> 
    </html>
     
    This will cause "targetframename" to request page2.asp, thus showing the response in that frame. 
     
    ...POST variables when doing a redirect? 
     
    When you submit a FORM with the GET method, your browser does not do anything clever, it simply appends the FORM elements to the end of the page in the ACTION parameter. When you submit a FORM using the POST method an entirely different mechanism is used. When your browser gets the 302 response, it reads the new location from the Location header and simply requests that page. You can simulate a GET submission by simply tagging on the parameters yourself, thereby emulating what the browser does. So if you do; 
     
    <% Response.Redirect "test2.asp?myparam=123" %>
     
    the resulting header looks like this; 
     
    HTTP/1.1 302 Object moved 
    Server: Microsoft-IIS/5.0 
    Date: Mon, 19 Mar 2001 15:16:35 GMT 
    Connection: close 
    Location: test2.asp?myparam=123 
    Content-Length: 130 
    Content-Type: text/html 
    Set-Cookie: ASPSESSIONIDQQGQQJWO=ANCJFABDFLBLHMKIJOIOKJDM; path=/ 
    Cache-control: private 
     
    <head><title>Object moved</title></head> 
    <body><h1>Object Moved</h1>This object may be found <a HREF="test2.asp?myparam=123">here</a>.</body>
     
    So the browser requests "test2.asp?myparam=123", thus passing on your parameters. Emulating a POST, however, is not that simple and the Redirect process simply does not support it. 
     
    The workaround is similar though, you should generate the appropriate HTML to do the job yourself; 
     
    <html> 
    <body> 
     
    <form name=frmTest action=page2.asp method=POST> 
    <input type=hidden name=txtMyVar value="Hello"> 
    </form> 
     
    <script> 
    document.frmTest.submit(); 
    </script> 
     
    </body> 
    </html>
     
    The only problem with this is that you are relying on scripting support from the client.

Is there an alternative?

This article submitted by Adrian Forbes - April 18, 2001

Related Articles

Can I bypass the ten connection limitation in Workstation / Professional?
Can I compact / repair an Access database from ASP code?
Can I create an array's size dynamically?
Can I detect the presence of ActiveX controls, like Flash, from ASP?
Can I dictate the load order of files on the client from ASP?
Can I have optional parameters to my subs / functions?
Can I host multiple sites in 2000 Workstation or XP Professional (e.g. PWS)?
Can I mimic trim / ltrim / rtrim in JScript?
Can I perform simple encryption / decryption in ASP?
Can I run IIS 5.0 / ASP 3.0 on Windows NT 4.0 or Windows 9x?
Can I run IIS on Windows Millennium or Windows XP Home?
Can I use IP address to uniquely identify visitors?
Does order matter when using different languages in ASP?
How can I give them a better 404 message?
How can I stop Photoshop from opening ASP files?
How can I track when my site is added to a user's favorites?
How do I access all active sessions on the server?
How do I access my server's registry from an ASP page?
How do I change a list into a set of table rows and columns?
How do I change document names / extensions in IIS / PWS?
How do I change the default server scripting language in InterDev?
How do I cloak / hide URL and QueryString information?
How do I comment blocks of ASP code?
How do I control access to an area?
How do I control printing from ASP?
How do I convert a name to proper case?
How do I convert exchange rates in ASP?
How do I convert from Hex to Int and back?
How do I convert numbers into words?
How do I convert old IDC / HTX pages to ASP?
How do I count the number of current users / sessions?
How do I count the number of times x occurs in string y?
How do I create a database from ASP?
How do I create my own blog?
How do I deal with disappearing application variables?
How do I decode an encoded URL?
How do I detect ENABLED cookies / javascript?
How do I detect the browser's encryption level / cipher strength?
How do I determine which version of IIS / ASP I'm running?
How do I disable the back/forward buttons?
How do I display the Euro symbol (€) in my ASP pages?
How do I embed a TAB character into source code?
How do I embed apostrophes (') and quotes (") in an HTML string?
How do I embed ASP delimiters (<% or %>) in a string?
How do I estimate the total size of my web page?
How do I execute a DOS command / batch file / exe from ASP?
How do I execute a ping command from ASP, and retrieve the results?
How do I find out the amount of space left on my server?
How do I fix the ::$DATA bug?
How do I FTP files from ASP?
How do I generate a treeview from ASP?
How do I generate unique GUIDs from ASP?
How do I get all the UBound() values in a multi-dimensional array?
How do I get IntelliSense to see ASP 3.0 methods?
How do I get my visitors' login name / username?
How do I get screen resolution from ASP?
How do I get the computer name / IP address of the server?
How do I get the server's timezone information?
How do I get the user's IP address or browser information?
How do I highlight words in a string?
How do I host multiple web sites on one IIS box?
How do I increase timeout values?
How do I iterate through session variables?
How do I know which version of VBScript my server is running?
How do I log / track ASP errors on my web site?
How do I make hyperlinks out of plain text URLs and e-mail addresses?
How do I make JavaScript send values to ASP?
How do I make my ASP page pause or 'sleep'?
How do I make my ASP page refresh?
How do I make my ASP pages more efficient?
How do I make search engines index pages with QueryStrings?
How do I make sure an entered string contains only valid characters?
How do I make sure my ASP question gets answered?
How do I make sure my servers have the same time?
How do I make sure people go to page x before page y?
How do I make sure the client is still connected before processing?
How do I make Visual InterDev's debugging features work?
How do I manage a session across multiple windows?
How do I pad digits with leading zeros?
How do I parse / analyze IIS Logs?
How do I parse the domain name out of a URL?
How do I parse the file name out of a path or URL?
How do I perform a Whois / DNS lookup from ASP?
How do I persist session state without cookies / session variables?
How do I prevent 'Invalid use of Null' errors?
How do I prevent my ASP pages from caching?
How do I prevent people from printing my ASP page?
How do I print the first n characters of a large block of text?
How do I prompt a "Save As" dialog for an accepted mime type?
How do I protect my ASP code?
How do I protect my client-side JavaScript code?
How do I protect my images and other visual content?
How do I put my ASP application onto a CD-Rom?
How do I read the contents of a remote web page?
How do I read the event log from ASP?
How do I redirect an http:// request to https://?
How do I refresh global.asa without restarting the application?
How do I round a number *properly* with VBScript?
How do I run ASP on other web servers besides IIS?
How do I schedule ASP files?
How do I send a MsgBox or InputBox from ASP?
How do I set session variables from client-side script?
How do I show a "Please Wait..." message?
How do I solve 'Event ID 5' errors?
How do I solve 'The Requested Resource is in Use' errors?
How do I solve 'The server failed to load the application' errors?
How do I solve 'The specified procedure could not be found' errors?
How do I solve ASP 0115 errors?
How do I specify ByRef / ByVal in VBScript?
How do I stress test my ASP application?
How do I time my ASP code?
How do I turn a KB Article #, like Q191987, into a usable URL?
How do I use ASP to [...]
How do I use extensions other than .ASP for ASP files?
How do I warn people when their session is about to expire?
How do I zip / unzip files from ASP?
I called Session.Abandon, why are my session variables still there?
I have plenty of RAM, why do I get an 'Out of memory' error?
Is there an easier way to patch my server(s)?
Should I use ADOVBS.inc for declaring constants?
Should I use sessionID to uniquely identify users?
Should I use the .inc extension for my include files?
Should I use VBScript or JScript for ASP?
What do I do when IIS 5.0 will not start?
What is Event ID 36, and how can I get IIS running again?
What is this 'Cannot detect OS type' error with NT 4.0 Option Pack?
What is this error 'An unhandled data type was encountered'?
What is wrong with IsNumeric()?
What is wrong with Request.ServerVariables("HTTP_REFERER")?
What is wrong with Session_OnEnd()?
What kind of object is Response.Crackers?
What's the deal with IIS 5.0 and ASP 3.0?
When I run a page in my browser, why does the ASP code not execute?
Where can I find out about .NET?
Where can I find out about running Perl in IIS?
Where can I host ASP pages for free (or at least cheap)?
Where do I get IIS / ASP?
Where else can I learn about ASP?
Which editor should I use for developing ASP applications?
Which is better, rs(0) or rs("column_name")?
Why am I getting 'subscript out of range' errors?
Why am I having problems installing Visual Studio.NET RTM?
Why am I having problems with Server.Execute and/or Server.Transfer?
Why are people telling me to fix my clock / timezone?
Why can't I browse localhost without an Internet connection?
Why can't I grab custom headers from Request.ServerVariables()?
Why can't I pass querystring information AND links to #bookmarks?
Why can't I turn buffering off using Response.Buffer?
Why can't I use #EXEC or #ECHO in an ASP page?
Why do I get 'BOF or EOF' errors?
Why do I get 'HTTP 500-12 Application Restarting' errors?
Why do I get 'HTTP/1.0 Invalid Application Name' errors?
Why do I get 'Invalid Default Script Language' errors?
Why do I get 'Name redefined' errors?
Why do I get 'Object doesn't support this property or method' errors?
Why do I get 'The RPC Server is Unavailable' messages?
Why do I get 'Type Mismatch' when using the Session object?
Why do I get 8000FFFF / 8002802B errors?
Why do I get 80010105 errors?
Why do I get 80010108 errors?
Why do I get 80020003 errors?
Why do I get 80020005 errors?
Why do I get 80020009 errors?
Why do I get 8002000E errors?
Why do I get 80029c84 errors?
Why do I get 8004E00F errors?
Why do I get 80070034 / 80070035 errors?
Why do I get 80070056 errors?
Why do I get 80070057 errors?
Why do I get 80090016 errors?
Why do I get 800A01C2 errors?
Why do I get 800A01C9 errors?
Why do I get 800A01CA errors?
Why do I get 800A01F4 errors?
Why do I get 800A01F9 errors?
Why do I get 800A03EC errors?
Why do I get 800A03ED / 800A03EE errors?
Why do I get 800A03F6 errors?
Why do I get 800A0401 errors?
Why do I get 800A0408 errors?
Why do I get 800A0414 errors?
Why do I get 800A138F errors?
Why do I get a 500 Internal Server error for all ASP errors?
Why do I get an 'overflow' error using CInt?
Why do I get an error about a 'Smart HTML interpreter'?
Why do I get ASP 0101 errors?
Why do I get ASP 0113 / Script timed out errors?
Why do I get ASP 0130 / ASP 0131 errors?
Why do I get ASP 0138 errors?
Why do I get ASP 0158 errors?
Why do I get errors in the 800A0001 -> 800A000F range?
Why do I get errors in the 800A0030 -> 800A003A range?
Why do I get errors in the 800A03F1 -> 800A03FF range?
Why do I get errors in the 800A0400 -> 800A041F range?
Why do I get errors when trying to redirect?
Why do I get HTTP/1.1 400 Bad Request errors?
Why do I get non-database-related 80004005 errors?
Why do I get script errors on one machine but not another?
Why do I get the error Object Required: ''?
Why do my session / application variables disappear?
Why does 3.2 + 1.5 = 4.7000000000000002?
Why does 4 / 5 = 0?
Why does global.asa not fire?
Why does GUID not work correctly with response.write?
Why does IIS hang and/or stop serving ASP pages?
Why does my page render (properly) in IE and not in Netscape?
Why does RecordCount return as -1?
Why does REMOTE_HOST return an IP address instead of a name address?
Why does session.abandon not take effect right away?
Why is Netscape slow in IIS 5.0?
Why won't my ASP pages work in IIS 6.0?
Why won't my session variables stick?
Why won't QueryString values work with Server.Execute / Server.Transfer?
Why won't Windows search ASP files?
DllHost.Exe

 

 


Created: 11/6/2001 | Last Updated: 11/6/2001 | broken links | helpful | not helpful | statistics
© Copyright 2006, UBR, Inc. All Rights Reserved. (473)

 

Copyright 1999-2006, All rights reserved.
Finding content
Finding content.  An error has occured...