How do I add custom headers to a request (ASP.Net 2.0)?
I have a need to add custom headers to a request before it is processed. I first created an HttpModule but quickly found that the context.Request.Headers collection is read-only.
After many failed ideas I attempted creating a custom DefaultHttpHandler. This custom DefaultHttpHandler overrides BeginProcessRequest().
I assumed that I would be able to write some code to add the custom headers into the request (or a copy of the request, or somewhere else in the context) before making a call to base.BeginProcessRequest() using my modified context object.
This seems to work some of the time when I add my headers to the base.ExecuteUrlHeaders collection.
But I’m not sure that I should be adding headers into an object that is not directly derived from the context. Also the base.ExecuteUrlHeaders is not always instantiated when my custom DefaultHttpHandler is called, it can be null, and
quite often is.
Is there a proper way to add headers to a request object using an HttpModule or HttpHandler?
David Wang (awesome guy) alludes to the fact that it can be done in many posts, but I’m having a problem getting the subtitle points worked out. I’m using IIS6 with ASP.NET 2.0
Oh yea, I wrote an ISAPI filter that does the job just fine, why is it so hard to get the same thing working in .Net?
I read your paragraph but am just going to address the question in the first sentence. I want to help you get your application working but suggest that you avoid this method. With that, [:)], this example will insert a new header as
early in the pipeline as possible using a class that inherits from HttpApplication. You can rework it to go into a module if needed.
public class TheApp : HttpApplication
{
public TheApp()
{
this.BeginRequest += new EventHandler(TheApp_BeginRequest);
this.AuthorizeRequest += new EventHandler(TheApp_AuthorizeRequest);
}
void TheApp_BeginRequest(object sender, EventArgs e)
{
//get a reference
NameValueCollection headers = HttpContext.Current.Request.Headers;
//get a type
Type t = headers.GetType();
//get the property
PropertyInfo p = t.GetProperty("IsReadOnly", BindingFlags.Instance | BindingFlags.IgnoreCase | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy);
//unset readonly
p.SetValue(headers, false, null);
//add a header
headers.Add("New Header", "New Value");
}
void TheApp_AuthorizeRequest(object sender, EventArgs e)
{
//look for new header
if(HttpContext.Current.Request.Headers["New Header"] != null)
{
HttpContext.Current.Response.Write("done.");
HttpContext.Current.Response.End();
}
}
}
I am also trying the same. Now problem is, i want to set header value in a page aspx page and want to excess in another aspx page. Can please help me by giving the proper code. I am somewhat confused. The following is my code. Thxs in
advance.
Page1.cs
public partial class test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
test123 te = new test123();
Server.Transfer("test2.aspx", true);
}
}
}
public class test123 : HttpApplication
{
public test123()
{
this.BeginRequest += new EventHandler(TheApp_BeginRequest);
}
I was grappling with this, for the purposes of forcing my page to expire, and getting the IIS pipeline errors and unsupported platform errors, and eventually it turned out to be as easy as calling the following:
Response.AddHeader(
"Expires",
"Fri, 30 Oct 1998 14:19:41 GMT");
or the equivalent.
I had a peek inside the .net method using Reflector and it seems to save everything up for the end, so avoiding errors being thrown.
Sorry - realised what you were trying to do! Hope the post is helpful for other people anyway,
You may have resolved this already. But just in case
You get this message ( error message ) if your project is file-system based. Convert it to a web-app and run it against IIS, above code works fine (at least in my case it worked fine :-) )
hi WolfeMan, I tried your code but got the exception saying "Operation not supported on this platform". I also tried deploying my web app on the IIS and got the same exception. I am developing on Windows XP Professional Service Pack 3 with IIS 5.1. Any work
arounds?
Telanas
Member
5 Points
1 Post
How do I add custom headers to a request (ASP.Net 2.0)?
Apr 06, 2006 10:47 PM|LINK
How do I add custom headers to a request (ASP.Net 2.0)?
I have a need to add custom headers to a request before it is processed. I first created an HttpModule but quickly found that the context.Request.Headers collection is read-only.
After many failed ideas I attempted creating a custom DefaultHttpHandler. This custom DefaultHttpHandler overrides BeginProcessRequest(). I assumed that I would be able to write some code to add the custom headers into the request (or a copy of the request, or somewhere else in the context) before making a call to base.BeginProcessRequest() using my modified context object.
This seems to work some of the time when I add my headers to the base.ExecuteUrlHeaders collection. But I’m not sure that I should be adding headers into an object that is not directly derived from the context. Also the base.ExecuteUrlHeaders is not always instantiated when my custom DefaultHttpHandler is called, it can be null, and quite often is.
Is there a proper way to add headers to a request object using an HttpModule or HttpHandler?
David Wang (awesome guy) alludes to the fact that it can be done in many posts, but I’m having a problem getting the subtitle points worked out. I’m using IIS6 with ASP.NET 2.0
Oh yea, I wrote an ISAPI filter that does the job just fine, why is it so hard to get the same thing working in .Net?
Thanks in advance!
WolfeMan
Member
50 Points
10 Posts
Re: How do I add custom headers to a request (ASP.Net 2.0)?
Apr 20, 2006 07:54 PM|LINK
Hello Telanas,
I read your paragraph but am just going to address the question in the first sentence. I want to help you get your application working but suggest that you avoid this method. With that, [:)], this example will insert a new header as early in the pipeline as possible using a class that inherits from HttpApplication. You can rework it to go into a module if needed.
public class TheApp : HttpApplication { public TheApp() { this.BeginRequest += new EventHandler(TheApp_BeginRequest); this.AuthorizeRequest += new EventHandler(TheApp_AuthorizeRequest); } void TheApp_BeginRequest(object sender, EventArgs e) { //get a reference NameValueCollection headers = HttpContext.Current.Request.Headers; //get a type Type t = headers.GetType(); //get the property PropertyInfo p = t.GetProperty("IsReadOnly", BindingFlags.Instance | BindingFlags.IgnoreCase | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy); //unset readonly p.SetValue(headers, false, null); //add a header headers.Add("New Header", "New Value"); } void TheApp_AuthorizeRequest(object sender, EventArgs e) { //look for new header if(HttpContext.Current.Request.Headers["New Header"] != null) { HttpContext.Current.Response.Write("done."); HttpContext.Current.Response.End(); } } }michael.rp
Member
4 Points
2 Posts
Re: How do I add custom headers to a request (ASP.Net 2.0)?
Mar 05, 2007 12:15 PM|LINK
WolfeMan:
Just what I was looking for. Thanks. [:)]
HttpModule HttpWebRequest
ketanmaheta
Member
133 Points
130 Posts
Re: How do I add custom headers to a request (ASP.Net 2.0)?
Aug 07, 2007 10:02 AM|LINK
Hi WolfeMan,
I am also trying the same. Now problem is, i want to set header value in a page aspx page and want to excess in another aspx page. Can please help me by giving the proper code. I am somewhat confused. The following is my code. Thxs in advance.
Page1.cs
public partial class test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
test123 te = new test123();
Server.Transfer("test2.aspx", true);
}
}
}
public class test123 : HttpApplication
{
public test123()
{
this.BeginRequest += new EventHandler(TheApp_BeginRequest);
}
void TheApp_BeginRequest(object sender, EventArgs e)
{
NameValueCollection headers = HttpContext.Current.Request.Headers;
Type t = headers.GetType();
PropertyInfo p = t.GetProperty("readOnly", System.Reflection.BindingFlags.IgnoreCase);// | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.FlattenHierarchy);
p.SetValue(headers, false, null);
headers.Add("Name", "ABCD");
}
}
Page2.cs
protected void Page_Load(object sender, EventArgs e)
{
string name = Page.Request.Headers["Name"];
}
willb75
Member
2 Points
3 Posts
Re: How do I add custom headers to a request (ASP.Net 2.0)?
Feb 20, 2008 11:01 AM|LINK
I was grappling with this, for the purposes of forcing my page to expire, and getting the IIS pipeline errors and unsupported platform errors, and eventually it turned out to be as easy as calling the following:
Response.AddHeader(
"Expires", "Fri, 30 Oct 1998 14:19:41 GMT");or the equivalent.
I had a peek inside the .net method using Reflector and it seems to save everything up for the end, so avoiding errors being thrown.
HttpPipeline asp.net 2.0 ASP.net 2.0 pipeline HTTP Headers Response Headers response Asp.net 2.0 response filters
udaykiranv
Member
2 Points
1 Post
Re: How do I add custom headers to a request (ASP.Net 2.0)?
Aug 08, 2008 06:20 AM|LINK
Micheal ....!
I tried doingh the same I am getting the following exception
System.PlatformNotSupportedException: Operation is not supported on this platform.
at System.Web.HttpHeaderCollection.Add(String name, String value)
at ASP.global_asax.Application_PreSendRequestHeaders(Object sender, EventArgs e)
Could you please let me know how did u get this (i e add a custom header to a request).[;)]
jayamaruthyr...
Member
4 Points
2 Posts
Re: How do I add custom headers to a request (ASP.Net 2.0)?
Aug 05, 2009 09:30 PM|LINK
You may have resolved this already. But just in case
You get this message ( error message ) if your project is file-system based. Convert it to a web-app and run it against IIS, above code works fine (at least in my case it worked fine :-) )
thanks
Jay
prashant kha...
Member
18 Points
16 Posts
Re: How do I add custom headers to a request (ASP.Net 2.0)?
Jul 19, 2010 12:50 PM|LINK
Hello
WolfMan,
I tried your sample code ,but can't add the header .
Any help regrading this would be appriciated.
Thanks in advance,
qijaz
Member
228 Points
51 Posts
Re: How do I add custom headers to a request (ASP.Net 2.0)?
Jul 20, 2010 02:33 PM|LINK
hi WolfeMan, I tried your code but got the exception saying "Operation not supported on this platform". I also tried deploying my web app on the IIS and got the same exception. I am developing on Windows XP Professional Service Pack 3 with IIS 5.1. Any work arounds?
Thanks.
Qasim.
malvar01
Member
2 Points
1 Post
Re: How do I add custom headers to a request (ASP.Net 2.0)?
Nov 10, 2010 03:39 PM|LINK
This is another way to add headers.
protected void Page_Load(object sender, EventArgs e)
{
string path = Request.Url.Scheme + "://" + Request.Url.Host + ":" + Request.Url.Port + "/";
myRedirect(path + "TestRedirectTo.aspx", "test", "testValue");
}
protected void myRedirect(string url, string headerName, string headerValue)
{
Response.Clear();
System.Net.WebRequest request = System.Net.WebRequest.Create(url);
request.Headers.Add(headerName, headerValue);
System.Net.WebResponse response = request.GetResponse();
System.IO.StreamReader sr = new System.IO.StreamReader(response.GetResponseStream(), System.Text.Encoding.Default);
string content = sr.ReadToEnd();
sr.Close();
Response.Write(content);
sr.Close();
Response.End();
}
<div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> protected void myRedirect(string url, string headerName, string headerValue)</div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> {</div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> Response.Clear();</div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> System.Net.WebRequest request = System.Net.WebRequest.Create(url);</div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> request.Headers.Add(headerName, headerValue);</div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> System.Net.WebResponse response = request.GetResponse();</div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> System.IO.StreamReader sr = new System.IO.StreamReader(response.GetResponseStream(), System.Text.Encoding.Default);</div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"></div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> string content = sr.ReadToEnd();</div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> sr.Close();</div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> Response.Write(content);</div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> sr.Close();</div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> Response.End();</div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> }</div>