locked
Add value to request header. RRS feed

  • Question

  • User672184119 posted

     I want to set header value  in a page aspx page and excess in another aspx page. The following is  my code. I am getting propertyInfo as null. Thxs in advance.

    Page1.cs

    public partial class test :  System.Web.UI.Page  
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                
    NameValueCollection headers = HttpContext.Current.Request.Headers;
                 Type t = headers.GetType();
                 PropertyInfo propertyInfo = t.GetProperty("IsReadOnly", System.Reflection.BindingFlags.IgnoreCase); |      System.Reflection.BindingFlags.Instance |    System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.FlattenHierarchy);
               
    propertyInfo.SetValue(headers, false, null);
                headers.Add("Name", "ABCD");
               Server.Transfer("Page2.aspx", true);
            }
        }

    }
     
    Page2.cs

     protected void Page_Load(object sender, EventArgs e)
     {
            string name = Page.Request.Headers["Name"];

     } 

     

    Wednesday, August 8, 2007 3:24 AM

Answers

  • User1191518856 posted

    I must back two steps and ask you what you are trying to achieve? Depending on what your requirements are, there might be better ways to solve this than going through page headers.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 9, 2007 3:58 AM

All replies

  • User1191518856 posted

    I must back two steps and ask you what you are trying to achieve? Depending on what your requirements are, there might be better ways to solve this than going through page headers.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 9, 2007 3:58 AM
  • User672184119 posted

     Hi,

              Actually one of our client is adding custom value to request header and redirecting to our application. For testing purpose we need to add this in test page and redirect to our application. So requirement is fixed. :)

    Thanks in advance,

    Ketan
     

    Tuesday, August 14, 2007 5:55 AM
  • User1191518856 posted

    I've tried your code now and it works perfectly on my machine. Although I had to correct an error in your code, where you set the BindingFlags. There was a semicolon in there which I had to remove. Correct line should be:

    PropertyInfo propertyInfo = t.GetProperty("IsReadOnly", System.Reflection.BindingFlags.IgnoreCase | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.FlattenHierarchy);
    
      
    Wednesday, August 15, 2007 3:01 AM
  • User672184119 posted

    Hi

    I corrected my code, but still i am getting "Operation is not supported on this platform." exception while adding value to header

    header.Add("Name","ABCD");

    can u please help me?

    Regards,

    Ketan

    Monday, September 3, 2007 8:12 AM
  • User-1924106670 posted

    Ketan

    Did you figure out how to get past the "Operation is not supported on this platform" exception?

    I am having the same problem.  This used to work for me, but I just ran a bunch of windows updates and am wondering if I broke my code by running those updates.

    Monday, September 24, 2007 12:37 PM
  • Thursday, October 18, 2007 6:20 PM
  • User-337341627 posted

     Hi all,

    I need to pass HTTP Headers from one web site to another web site as I
    need to post a form on one web applications page to page on different
    web site
    and user credentials are to be passed from HTTP headers.

    I can pass values through querystring but due to security reasons i am asked to do so. 

     

    Here is my code in website1

    NameValueCollection headers = HttpContext.Current.Response.Headers;
                 Type t = headers.GetType();
                 PropertyInfo propertyInfo = t.GetProperty("IsReadOnly", System.Reflection.BindingFlags.IgnoreCase | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.FlattenHierarchy);
                propertyInfo.SetValue(headers, false, null);
                headers.Add("UserEmail", objBasePage.CurrentEmailID); 
                  
                  
                    Response.Redirect("http://www.website2.com/login.aspx);

    In website 2
    string UserEmail = string.Empty;
                UserEmail = Page.Request.Headers["UserEmail"].ToString();

               Here it is throwing exception 'object reference cannot set to null';

    Please suggest me a way to come out of this.

    Thanks in advance

    sarika
     


     


     




    Thanks in advance

    Wednesday, October 22, 2008 4:35 AM
  • User338859378 posted

    Have you got the answer to your question or solution to your problem.

    Thursday, May 28, 2009 5:05 AM
  • User2139520849 posted

    Try this


    HttpApplication objApp = (HttpApplication)r_objSender;
    HttpRequest Request = (HttpContext)objApp.Context.Request;
    
    
    //get a reference
    NameValueCollection headers = Request.Headers;
    
    //get a type
    Type t = headers.GetType();
    System.Collections.ArrayList item = new System.Collections.ArrayList();
    
    t.InvokeMember("MakeReadWrite",BindingFlags.InvokeMethod | BindingFlags.NonPublic | BindingFlags.Instance,null,headers,null);
    t.InvokeMember("InvalidateCachedArrays",BindingFlags.InvokeMethod | BindingFlags.NonPublic | BindingFlags.Instance,null,headers,null);
    item.Add("CUSTOM_HEADER_VALUE");
    t.InvokeMember("BaseAdd",BindingFlags.InvokeMethod | BindingFlags.NonPublic | BindingFlags.Instance,null,headers, new object[]{"CUSTOM_HEADER_NAME",item});
    t.InvokeMember("MakeReadOnly",BindingFlags.InvokeMethod | BindingFlags.NonPublic | BindingFlags.Instance,null,headers,null); 




    Wednesday, July 21, 2010 12:00 PM