Cannot redirect after http headers have been sent

A

Andreas Klemt

Hello,

I get this error Message "cannot redirect after http headers have been sent"
when I do this
response.redirect ("home.aspx")

How can I find out with vb.net if already a http header has been sent like

If response.IsSentHttpHeader then
....
End If.

Thanks for any help in advance!
Andreas Klemt
 
S

spalding

Andreas said:
*Hello,

I get this error Message "cannot redirect after http headers hav
been sent"
when I do this
response.redirect ("home.aspx")

How can I find out with vb.net if already a http header has been sen
like

If response.IsSentHttpHeader then
.....
End If.

Thanks for any help in advance!
Andreas Klemt *

Hi

Are you writing anything out to the Response stream prior to callin
Redirect;

eg.

Response.Write("this is a test");
//some more code...
Response.Redirect(strNewASPPage);

If so the first Response.Write will send a response to request heade
to the browser.

If you then call Response.Redirect, you are sending a different heade
to the browser. This is an error. You can't send two different header
to the browser


-
spaldin
 
J

Joe Fallon

I have this problem also.

And yes, I believe that is exactly how it is caused.

The question now is:
how do I detect this condition?

I have used Try Catch to eat the exception and this does not work reliably
either.
I usually end up in my Global error handler and any attempt th re-direct to
my custom error page causes this exception. It is quite frustrating.

I believe the cause is using Ben Lowery's httpCompression module. This is an
excellent module for compressing data sent over the wire but when I get an
error and try to re-direct to my custom page it is "too late" because the
Headers have been sent by the compression module.

I would dearly love a solution to this issue as otherwise I may have to
remove the compression feature.
 
C

Chandra Sekhar

Try Response.ClearContents and ClearHeaders. If all else fails, do a client
side window.location.replace or navigate - with a registerclientscript on
server side.

Sekhar.


Joe Fallon said:
I have this problem also.

And yes, I believe that is exactly how it is caused.

The question now is:
how do I detect this condition?

I have used Try Catch to eat the exception and this does not work reliably
either.
I usually end up in my Global error handler and any attempt th re-direct to
my custom error page causes this exception. It is quite frustrating.

I believe the cause is using Ben Lowery's httpCompression module. This is an
excellent module for compressing data sent over the wire but when I get an
error and try to re-direct to my custom page it is "too late" because the
Headers have been sent by the compression module.

I would dearly love a solution to this issue as otherwise I may have to
remove the compression feature.
 
S

spalding

Hi Joe and Andreas

In what way are you using Response.Write and Response.Redirect at th
same time?

Joe, you mentioned that you were using a compression class to outpu
your data. Does it use the Response object in any way?

Would you be able to post a code snippet so we can see exactly how you
code is structured?

Chandra Sekhar has suggested a good solution; ClearHeaders o
ClearContent. Just make sure BufferOutput is true (it is by default).
Javascript redirect is good but you don't know if it is disabled on th
client (and with many recent security issues it may be).

Alternatively, write your output to a string variable then use a
if...else statement for calling either Response.Write o
Response.Redirect.

Joe said:
*I have this problem also.

And yes, I believe that is exactly how it is caused.

The question now is:
how do I detect this condition?

I have used Try Catch to eat the exception and this does not wor
reliably
either.
I usually end up in my Global error handler and any attempt t
re-direct to
my custom error page causes this exception. It is quite frustrating.

I believe the cause is using Ben Lowery's httpCompression module
This is an
excellent module for compressing data sent over the wire but when
get an
error and try to re-direct to my custom page it is "too late" becaus
the
Headers have been sent by the compression module.

I would dearly love a solution to this issue as otherwise I may hav
to
remove the compression feature.


-
spaldin
 
J

Joe Fallon

========================================
The source for the compression module is located here:
http://www.blowery.org/code/HttpCompressionModule.html

Note: Ben Lowery fiixed the code 4 months ago to work with Server.Transfer
and Response.Redirect.
I think my case is not part of his fix.

/// An HttpModule that hooks onto the Response.Filter property of the
/// current request and tries to compress the output, based on what
/// the browser supports.

///This module checks the Accept-Encoding HTTP header to determine if the
/// client actually supports any notion of compression. Currently, we
support
/// the deflate (zlib) and gzip compression schemes.
========================================
When I have it enabled in my web.config, the pages are sent to the browser
in compressed form.
But the module must be sending the headers "early" in the process. When I
run into an unhandled error, I log it and clear it in Global.asax and then
re-direct to my custom error form. This re-direct is what causes the dreaded
"http headers have already been sent" error. I am using a Try catch block
around my re-direct in Global.asax in an attempt to "eat the error".
========================================
I tried the settings ClearHeaders or ClearContent. But it did not stop the
error from occurring.
========================================
In Global.asax I have this: (remember, I was trying to load a page and got
an unhandled error)
========================================
'log error here. Then:

Server.ClearError()

'JF 8/9/2004 - See if this will prevent the error for "Server cannot append
header after HTTP headers have been sent." Nope.
Response.ClearContent()
Response.ClearHeaders()

Dim strErrorMessage As String = "We're sorry, but an unhandled error
occurred on the server. The Server Administrator has been notified and the
error logged."

Try
'this shared method sets some variables and then re-directs to my
standard error page.
MyUtilClass.DisplayErrorMessage("Application Error", strErrorMessage)
Catch except As Exception
'do nothing
End Try
========================================

Thanks for any advice.
 
S

spalding

Hi Joe

Apologies for not getting back to you sooner.

Okay;

Response.ClearHeaders and Response.ClearContents will only work i
Response.BufferOutput is true (which is by default). In fact, i
BufferOutput is set to false, the Response object will send data to th
browser immediately on every call.

Does your code set the BufferOutput field to false?
eg. Response.BufferOutput = false;

If so, you will not be able to call Write and then call Redirect in th
same code block.

If the compression utility is doing it, then you will need to hack th
above change out of it.

However, the problem does not occur with Server.Transfer()
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top