How to add custom key in HttpRequest Header?

Posted by Hmanjarawala under ASP.NET on 8/19/2011 | Points: 10 | Views : 4809 | Status : [Member] | Replies : 6
Hi all,

I'm trying add custom key in HttpRequest herder.

I tried to inherit HttpApplication class and overrides its BeginRequest event and do same with IHttpModule interface, but failed in doing so.

I frequently get errors like "Operation is not supported on this plateform".

will you all plz help me to sort out this problem?

Himanshu Manjarawala
Sr. Software Engineer@AutomationAnywhere
http://fieredotnet.wordpress.com/



Responses

Posted by: Dhirendra on: 8/19/2011 [Member] Starter | Points: 25

Up
0
Down
do you want to add custom key in httpResponse header ? httprequest object will be readonly and you might not be able to add any key or it will not be use full...


Hmanjarawala, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Hmanjarawala on: 8/19/2011 [Member] Bronze | Points: 25

Up
0
Down
No, not in HttpResponse, I want to add value in HttpRequest Header.

Act. i want to pass some information to ISAPI filters, but not through Query strings due to security reason.

So final way is to pass that value through HttpRequest Header.

plz guide me to resolve this issue

Himanshu Manjarawala
Sr. Software Engineer@AutomationAnywhere
http://fieredotnet.wordpress.com/

Hmanjarawala, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Dhirendra on: 8/19/2011 [Member] Starter | Points: 25

Up
0
Down
Ok..here is the solution...
you have to create custom httpmodule
add handler to begin request method..
in begin request method...get all headers and set readonly only property to false through reflection..
add custom header in NameValueCollection-->NameObjectCollectionBase' objtec's collection which ultimately add the header in request object's header..
Following is the code of httpmodule..
public class MyModule1 : IHttpModule
{
public void Dispose()
{
//clean-up code here.
}

public void Init(HttpApplication context) {

context.BeginRequest += new EventHandler(context_BeginRequest);
}

void context_BeginRequest(object sender, EventArgs e)
{
var headers = HttpContext.Current.Request.Headers;
Type hdr = headers.GetType();
PropertyInfo ro = hdr.GetProperty("IsReadOnly", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.IgnoreCase | BindingFlags.FlattenHierarchy);
// Remove the ReadOnly property
ro.SetValue(headers, false, null);
// Invoke the protected InvalidateCachedArrays method
hdr.InvokeMember("InvalidateCachedArrays",
BindingFlags.InvokeMethod | BindingFlags.NonPublic | BindingFlags.Instance,
null, headers, null);
// Now invoke the protected "BaseAdd" method of the base class to add the
// headers you need. The header content needs to be an ArrayList or the
// the web application will choke on it.
hdr.InvokeMember("BaseAdd", BindingFlags.InvokeMethod | BindingFlags.NonPublic | BindingFlags.Instance, null, headers, new object[] { "CustomHeaderKey", new ArrayList { "CustomHeaderContent" } });
// repeat BaseAdd invocation for any other headers to be added
// Then set the collection back to ReadOnly
ro.SetValue(headers, true, null);

}

}
following is the web.config setting needs to add
<httpModules >
<add name="MyModule1" type="WebApplication70515.MyModule1"/>
</httpModules>

aspx page on load code to see if the header is set or not
protected void Page_Load(object sender, EventArgs e)
{
if (Request.Headers["CustomHeaderKey"] != null)
{
Response.Write(Request.Headers["CustomHeaderKey"]);
}
}

Please mark as answer if the above solution is helped to you.......

Hmanjarawala, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Hmanjarawala on: 8/20/2011 [Member] Bronze | Points: 25

Up
0
Down
Hi dhirendra,

Thanks for your help.

now real problem begins, act i want to user this Custom Header Variable in ISAPI Filter which i developerd in VC++ 6.0.

i want to use it in "OnPreprocHeaders" events.

i have written code for same, but didn't get this variable there.

will you help me in that context??

Himanshu Manjarawala
Sr. Software Engineer@AutomationAnywhere
http://fieredotnet.wordpress.com/

Hmanjarawala, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Hmanjarawala on: 8/20/2011 [Member] Bronze | Points: 25

Up
0
Down
Hi dhirendra,

will you tell me what is this "WebApplication70515".

caz if i use my Application name here then it gives error.

so what is it?

Himanshu Manjarawala
Sr. Software Engineer@AutomationAnywhere
http://fieredotnet.wordpress.com/

Hmanjarawala, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Dhirendra on: 8/20/2011 [Member] Starter | Points: 25

Up
0
Down
yes..it is the application name which might be the same as the defaut namespace added by .net.
use your application name + namespace of the httpmodule class otherwise add the same namespace to your httpmodule class

Hmanjarawala, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response