locked
Add/Modify the [key, value] pairs of HttpContext.Request.ServerVariables in C# RRS feed

  • Question

  • User1611510683 posted

    Hi,

    How do I add custom headers/values to a Request object in ASP.NET 1.1? I created an HttpModule.I took the Headers in a NameValueCollection I was able to add the header and the value to the collection. But i am not able to reflect this back to the request object. I also need to do the same thing with the ServerVariables. Tried the same by coding it in PreSendRequestHeaders(). Added a custom key and value pair to it. It doesnt throw an error. But neither does it get added to the collection. How do i proceed ?

    1    using System;
    2    using System.Web;
    3    using System.Collections.Specialized;
    4    using System.Reflection;
    5    namespace RequestModuleProject
    6    {
    7    	/// <summary>
    8    	
    9    	public class RequestModule : IHttpModule
    10   	{
    11   		public RequestModule()
    12   		{				
    13   		}
    14   
    15   		public string ModuleName
    16   		{
    17   			get { return "RequestModule"; }
    18   		}
    19   
    20   		public void Init(HttpApplication app)
    21   		{
    22   			app.BeginRequest +=new EventHandler(app_BeginRequest);			
    23   			app.AuthorizeRequest +=new EventHandler(app_AuthorizeRequest);
    24   			app.PreSendRequestHeaders +=new EventHandler(app_PreSendRequestHeaders);
    25   		}
    26   		
    27   
    28   		public void app_PreSendRequestHeaders(object sender,System.EventArgs e)
    29   		{
    30   			HttpApplication hp = (HttpApplication) sender;
    31   			HttpContext context = hp.Context;
    32   
    33   			NameValueCollection heads = context.Request.ServerVariables;
    34   						
    35   			Type t = heads.GetType();
    36   			PropertyInfo p = t.GetProperty("IsReadOnly", BindingFlags.Instance | BindingFlags.IgnoreCase | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy);
    37   			p.SetValue(heads, false, null);
    38   			heads.Add("New Header value", "New Value SV");
    39   			foreach(string keys in heads.Keys)
    40   			{
    41   				context.Response.Write(string.Format("<i>{0}</i> = {1}<br>",keys,heads[keys]));				
    42   			}
    43   		}
    44   
    45   		private void app_BeginRequest(object sender,System.EventArgs e)
    46   		{
    47   			HttpApplication hp = (HttpApplication) sender;
    48   			HttpContext context = hp.Context;
    49   			context.Response.Write("Add Request");
    50   			//Add the header n the name value collection
    51   
    52   			NameValueCollection headers = context.Request.Headers;
    53   			Type t = headers.GetType();
    54   			//get the property
    55   			PropertyInfo p = t.GetProperty("IsReadOnly", BindingFlags.Instance | BindingFlags.IgnoreCase | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy);
    56   			//unset readonly
    57   			p.SetValue(headers, false, null);
    58   			//add a header
    59   			headers.Add("HTTP_Header", "HTTP_Value");
    60   			
    61   			
    62   
    63   			foreach(string key in HttpContext.Current.Request.ServerVariables.Keys)
    64   			{
    65   				context.Response.Write(string.Format("<b>{0}</b> = {1}<br>",key,HttpContext.Current.Request.ServerVariables[key]));
    66   			}
    67   
    68   			context.Response.Write("--------------------------------<br>");
    69   			foreach(string key in headers.Keys)
    70   			{
    71   				context.Response.Write(string.Format("<b>{0}</b> = {1}<br>",key,headers[key]));
    72   
    73   			}
    74   			context.Response.Write("--------------------------------<br>");
    75   
    76   			
    77   		}
    78   
    79   		   
    80   		public void Dispose()
    81   		{			
    82   		}
    83   	}
    84   }
    85   
    
     Please help.. PS: Please find the code below of the HttpModule 
    Friday, August 10, 2007 8:47 AM

Answers

All replies

  • User-900677162 posted

    HI,deephab:

    You can check this thread:

    http://forums.asp.net/t/1125149.aspx

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, August 12, 2007 11:15 PM
  • User-225114762 posted

    Are you really sure you really want to do this? Although it's probably possible in some scenarios using reflection, perhaps by doing it like suggested in the thread referred to by another poster, It's A Bad Idea, TM.

    You're depending on intimate knowledge of something that is likely to change in the future, you're requiring your code to run with effectively full trust etc. The only semi-legitimate reason I can think of is if you're interfacing with legacy code that you do not have access to the source code to, and the only available interface to that code is via request headers/server variables, and you can't get the client and server to provide them as intended...

    If you do have access to the source of the receiving code, I don't think that "it's too much work to change" is a valid reason to produce such dependencies.

    Then again, this might be in a totally different scenario, and the need to to this is perfectly legit and the dependency issue moot. If so, try the reflection hack suggested in the other thread. Should that run into difficulties, bring out your trusty reflector and figure out how these collections work, and thus how to hack them.

    Otherwise, HttpContext.Items is the accepted mechanism to communicate between HttpModules and HttpHandlers.

    Tuesday, August 14, 2007 2:18 AM
  • User1919589733 posted
    I have a situation where IBM's Tivoli Access Manager injects server variables for me to grab out in a production environment.  However, it is very tedious to set up a development environment to do this.  Therefore in when I develop I want to inject those variables so my web app works similar to the prod implementation. 
    Friday, August 24, 2007 11:57 AM