472,128 Members | 1,703 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,128 software developers and data experts.

Cannot redirect after http headers have been sent

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
Nov 18 '05 #1
8 24047
I always ask a question. How many pages have this problem?

chanmm

"Andreas Klemt" <ak******@hotmail.com> wrote in message
news:uO**************@tk2msftngp13.phx.gbl...
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

Nov 18 '05 #2
chanmmn, do you have a solution?
"chanmmn" <ch*****@hotmail.com> schrieb im Newsbeitrag
news:%2*****************@TK2MSFTNGP12.phx.gbl...
I always ask a question. How many pages have this problem?

chanmm

"Andreas Klemt" <ak******@hotmail.com> wrote in message
news:uO**************@tk2msftngp13.phx.gbl...
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


Nov 18 '05 #3

Andreas Klemt wrote:
*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
-----------------------------------------------------------------------
Posted via http://www.codecomments.co
-----------------------------------------------------------------------

Nov 18 '05 #4
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.

--
Joe Fallon

"spalding" <sp*************@mail.codecomments.com> wrote in message
news:sp*************@mail.codecomments.com...

Andreas Klemt wrote:
*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 *


Hi

Are you writing anything out to the Response stream prior to calling
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 header
to the browser.

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

--
spalding
------------------------------------------------------------------------
Posted via http://www.codecomments.com
------------------------------------------------------------------------

Nov 18 '05 #5
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" <jf******@nospamtwcny.rr.com> wrote in message
news:el**************@TK2MSFTNGP12.phx.gbl...
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.

--
Joe Fallon

"spalding" <sp*************@mail.codecomments.com> wrote in message
news:sp*************@mail.codecomments.com...

Andreas Klemt wrote:
*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 *


Hi

Are you writing anything out to the Response stream prior to calling
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 header
to the browser.

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

--
spalding
------------------------------------------------------------------------
Posted via http://www.codecomments.com
------------------------------------------------------------------------


Nov 18 '05 #6

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 Fallon wrote:
*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.

--
Joe Fallon

"spalding" <sp*************@mail.codecomments.com> wrote in message
news:sp*************@mail.codecomments.com...

Andreas Klemt wrote:

Hi

Are you writing anything out to the Response stream prior t

calling
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 reques

header
to the browser.

If you then call Response.Redirect, you are sending a differen

header
to the browser. This is an error. You can't send two differen

headers
to the browser.

--
spalding


------------------------------------------------------------------------
Posted via http://www.codecomments.com


------------------------------------------------------------------------

-
spaldin
-----------------------------------------------------------------------
Posted via http://www.codecomments.co
-----------------------------------------------------------------------

Nov 18 '05 #7
========================================
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.
--
Joe Fallon


"spalding" <sp*************@mail.codecomments.com> wrote in message
news:sp*************@mail.codecomments.com...

Hi Joe and Andreas

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

Joe, you mentioned that you were using a compression class to output
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 your
code is structured?

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

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

Joe Fallon wrote:
*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.

--
Joe Fallon

"spalding" <sp*************@mail.codecomments.com> wrote in message
news:sp*************@mail.codecomments.com...
>
> Andreas Klemt wrote:
>
> Hi
>
> Are you writing anything out to the Response stream prior to

calling
> 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

header
> to the browser.
>
> If you then call Response.Redirect, you are sending a different

header
> to the browser. This is an error. You can't send two different

headers
> to the browser.
>
>
>
> --
> spalding
>

------------------------------------------------------------------------
> Posted via http://www.codecomments.com
>

------------------------------------------------------------------------
> *


--
spalding
------------------------------------------------------------------------
Posted via http://www.codecomments.com
------------------------------------------------------------------------

Nov 18 '05 #8

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()
Joe Fallon wrote:
*========================================
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 wit
Server.Transfer
and Response.Redirect.
I think my case is not part of his fix.

/// An HttpModule that hooks onto the Response.Filter property o
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 i
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 th
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 an
then
re-direct to my custom error form. This re-direct is what causes th
dreaded
"http headers have already been sent" error. I am using a Try catc
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 no
stop the
error from occurring.
========================================
In Global.asax I have this: (remember, I was trying to load a pag
and got
an unhandled error)
========================================
'log error here. Then:

Server.ClearError()

'JF 8/9/2004 - See if this will prevent the error for "Server canno
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 notifie
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.
--
Joe Fallon


"spalding" <sp*************@mail.codecomments.com> wrote in message
news:sp*************@mail.codecomments.com...

Hi Joe and Andreas

In what way are you using Response.Write and Response.Redirect a

the
same time?

Joe, you mentioned that you were using a compression class t

output
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 ho

your
code is structured?

Chandra Sekhar has suggested a good solution; ClearHeaders or
ClearContent. Just make sure BufferOutput is true (it is b

default). A
Javascript redirect is good but you don't know if it is disabled o

the
client (and with many recent security issues it may be).

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

Joe Fallon wrote:

--
spalding


------------------------------------------------------------------------
Posted via http://www.codecomments.com

------------------------------------------------------------------------
*


--
spalding
------------------------------------------------------------------------
Posted via http://www.codecomments.com
------------------------------------------------------------------------

Nov 18 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Sam | last post: by
reply views Thread by Hai Nguyen | last post: by
4 posts views Thread by craigtomo | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.