• 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);
            }
  • 相关阅读:
    开启mysql远程登录
    InfluxDB安装及配置
    基于ssh反向代理实现的远程协助
    小工具之进程守护器
    生成freeswitch事件的几种方式
    freeswitch模块之event_socket
    freeswitch对接其它SIP设备
    freeswitch注册过程分析
    redis参考文档
    创建型模式之Builder模式及实现
  • 原文地址:https://www.cnblogs.com/nanfei/p/7891525.html
Copyright © 2020-2023  润新知