zoukankan      html  css  js  c++  java
  • asp.net 动态更改 Request.Header

    public class Dev_Sim: IHttpModule
    {
        public void Init(HttpApplication app)
        {
            app.BeginRequest += delegate { app.Context.Request.Headers.Add("UserName", "XYZZY"); };
        }
    
        public void Dispose(){}
    }
    void changeHeader(HttpApplication app)
            {
                var headers = app.Context.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("BaseRemove",
                    BindingFlags.InvokeMethod | BindingFlags.NonPublic | BindingFlags.Instance,
                    null, headers,
                    new object[] { "User-Agent" });
                hdr.InvokeMember("BaseAdd",
                    BindingFlags.InvokeMethod | BindingFlags.NonPublic | BindingFlags.Instance,
                    null, headers,
                    new object[] { "User-Agent", 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);
            }
  • 相关阅读:
    JAVA(五)反射机制/Annotation
    JAVA(四)类集/枚举
    JAVA(三)JAVA常用类库/JAVA IO
    JAVA(二)异常/包及访问权限/多线程/泛型
    JAVA(一)JAVA基础/面向对象基础/高级面向对象
    【Android】Android输入子系统
    【Linux】深入理解Linux中内存管理
    【XMPP】基于XMPP的即时通讯解决方案
    cf593d
    cf593c
  • 原文地址:https://www.cnblogs.com/nanfei/p/7891525.html
Copyright © 2011-2022 走看看