日期:2014-05-18  浏览次数:20473 次

急救!如何转发Reqeust?
现在我是想实现这样一个功能,当有人访问A站点的一个页面的时候,我想把这个request同时转发到我另外一个站点B的同样一张页面上,该如何操作呢?A和B是部署的同样两份webapplication

------解决方案--------------------
你这样做有什么意义?

可以在Global.asax

C# code

  void Application_BeginRequest(Object sender, EventArgs e)
    {
        //这里把你的请求转发到站点B
    }

------解决方案--------------------
C# code

    void Application_BeginRequest(Object sender, EventArgs e)
    {
        HttpApplication application = sender as HttpApplication;

        HttpRequest request = application.Request;

        string domain = "http://www.b.com";
        string url = domain + request.Url.AbsolutePath + request.Url.Query;
        System.Net.HttpWebRequest webRequest = System.Net.WebRequest.Create(url) as System.Net.HttpWebRequest;

        if (request.HttpMethod == "GET")
        {
            //模拟个包

            webRequest.Accept = "*/*";
            webRequest.UserAgent = request.UserAgent;
            foreach(string head in request.Headers)
                webRequest.Headers.Add(head,request.Headers[head]);
            
            //还有其它的也加进去
        }
        else if (request.HttpMethod == "POST")
        { 
            //同理
        }
        
    }