Adding header to request in HTTP Module

N

nsyforce

Can you add a header to a request in an http module? I'm trying
unsuccessfully. Here's an example of my code:

public void Init(HttpApplication r_objApplication)
{

r_objApplication.EndRequest += new
EventHandler(this.EndRequest)
}

protected void EndRequest(object sender, EventArgs e)
{
HttpResponse res = HttpContext.Current.Response;
res.AddHeader("MyTestHeaderVar","MyTestVarValue");
res.Output.Flush();
}

My test page then spits out all request.headers and all
request.servervariables and my header is not anywhere. Does anyone
know what I'm doing wrong?

Thanks in advance.
 
B

Brock Allen

If the page isn't buffered, then this should work. On an unrelated note (or
maybe it is?), you don't need to call:

res.Output.Flush();
 
K

Kevin Spencer

Headers are not visible in a browser. They are part of an HTTP message that
is not in the HTML of the message.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
The sun never sets on
the Kingdom of Heaven
 
J

Juan T. Llibre

Hi.

The ServerVariables collection returned by the Request object
only contains headers sent from the browser to the server.

Since your custom header was created on the server and then
sent to the browser (the opposite direction), your header will
never be added to the ServerVariables collection.

You should be aware of this if you ever plan to interrogate the
ServerVariables collection, expecting to find a custom header
you created in this way. It will never be there.

To check for custom headers added, use a tool like ieHttpHeaders v 1.6,
which shows the http headers in IE.

http://www.blunck.info/iehttpheaders.html

It's quite convenient to use when debugging...and it's free!
 

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,969
Messages
2,570,161
Members
46,705
Latest member
Stefkari24

Latest Threads

Top