Ejemplo n.º 1
0
        protected override void OnReceivedContext(ReceivedContextEventArgs e)
        {
            var req = WebRequest.Create(ForwardUrl + e.Context.Request.Url.OriginalString) as HttpWebRequest;
            req.Method = e.Context.Request.Method;
            SetMessageHeader(e.Context.Request.Headers, req);
            SetMessageHeader(OverrideHeader, req);

            try
            {
                var res = req.GetResponse();
                var resStrm = res.GetResponseStream();
                var outStrm = e.Context.Response.GetOutputStream();
                outStrm.Write(resStrm);
            }
            catch (WebException ex)
            {
                var res = (HttpWebResponse)ex.Response;
                if (res != null)
                {
                    e.Context.Response.ProtocolVersion = new Version("1.1");
                    e.Context.Response.StatusCode = res.StatusCode;
                    foreach (var str in res.Headers.AllKeys)
                    {
                        e.Context.Response.Headers[str] = ex.Response.Headers[str];
                    }
                    using (var outStrm = e.Context.Response.GetOutputStream())
                    {
                        using (var inStrm = res.GetResponseStream())
                        {
                            byte[] buffer2 = new byte[1024];
                            int num2 = 0;
                            while ((num2 = inStrm.Read(buffer2, 0, buffer2.Length)) > 0)
                            {
                                outStrm.Write(buffer2, 0, num2);
                            }
                        }
                        return;
                    }
                }
                e.Context.Response.StatusCode = HttpStatusCode.BadGateway;
            }
        }
 protected virtual void OnReceivedContext(ReceivedContextEventArgs e)
 {
     if (ReceivedContext != null)
         ReceivedContext(this, e);
 }
 protected void OnPassingContext(ReceivedContextEventArgs e)
 {
     if (PassingContext != null)
         PassingContext(this, e);
 }
 public override void OnCalledService(HttpContext context)
 {
     HttpWebResponse response = null;
     try
     {
         if (context.Request.Method == "CONNECT")
             throw new IOException();
         var args = new ReceivedContextEventArgs(context);
         OnReceivedContext(args);
     }
     finally
     {
         if (response != null)
         {
             response.Close();
         }
     }
 }