public void Start()
        {
            listener.Start();
            try
            {
                while (true)
                {
                    Console.WriteLine("Listening...");
                    var context = listener.GetContext();//blocks while waiting for a request.
                    var requestString = context.Request.RawUrl;
                    //var request = context.Request;
                    Console.WriteLine("Got request for " + requestString);

                    try
                    {

                        var request = WebRequest.Create(requestString) as HttpWebRequest;
                        request.AllowAutoRedirect = false;
                        request.KeepAlive = context.Request.KeepAlive;
                        request.Proxy = new WebProxy();
                        request.Proxy.Credentials = CredentialCache.DefaultCredentials;
                        request.Method = context.Request.HttpMethod;
                        request.ContentType = context.Request.ContentType;

                        var cookies = new CookieContainer();
                        cookies.Add(new Uri(requestString), context.Request.Cookies);
                        request.CookieContainer = cookies;
                        request.Timeout = 20000;

                        var rs = new RequestData(request, context);
                        if (request.Method != "GET")
                        {
                            request.ContentLength = context.Request.ContentLength64;
                            request.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), rs);
                        }
                        else
                        {
                            IAsyncResult result = (IAsyncResult)request.BeginGetResponse(
                            new AsyncCallback(RespCallback), rs);
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
Beispiel #2
0
        static void Main(string[] args)
        {
            // Create a listener.
            HttpListener listener = new HttpListener();
            listener.Prefixes.Add("http://*:8082/");

            listener.Start();
            try
            {
                while (true)
                {
                    Console.WriteLine("Listening...");
                    // Note: The GetContext method blocks while waiting for a request.
                    HttpListenerContext context = listener.GetContext();
                    string requestString = context.Request.RawUrl;
                    Console.WriteLine("Got request for " + requestString);
                    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(requestString);
                    request.KeepAlive = true;
                    request.Proxy.Credentials = CredentialCache.DefaultCredentials;
                    request.Timeout = 200000;

                    RequestData requestData = new RequestData(request, context);
                    IAsyncResult result = (IAsyncResult)request.BeginGetResponse(new AsyncCallback(RespCallback), requestData);
                }
            }
            catch (WebException e)
            {
                Console.WriteLine("\nMain Exception raised!");
                Console.WriteLine("\nMessage:{0}", e.Message);
                Console.WriteLine("\nStatus:{0}", e.Status);
            }
            catch (Exception e)
            {
                Console.WriteLine("\nMain Exception raised!");
                Console.WriteLine("Source :{0} ", e.Source);
                Console.WriteLine("Message :{0} ", e.Message);
            }

            listener.Stop();
        }
        private void RespCallback(RequestData requestData, HttpWebResponse response)
        {
            try
            {
                Console.WriteLine("Got back response from " + requestData.Context.Request.Url.AbsoluteUri);

                using (response)
                using (Stream receiveStream = response.GetResponseStream())
                {
                    HttpListenerResponse responseOut = requestData.Context.Response;

                    // Need to get the length of the response before it can be forwarded on
                    responseOut.ContentLength64 = response.ContentLength;
                    int bytesCopied = CopyStream(receiveStream, responseOut.OutputStream);

                    responseOut.OutputStream.Close();

                    Console.WriteLine("Copied {0} bytes", bytesCopied);
                }
            }
            catch (WebException e)
            {
                Console.WriteLine("\nMain Exception raised!");
                Console.WriteLine("\nMessage:{0}", e.Message);
                Console.WriteLine("\nStatus:{0}", e.Status);
            }
            catch (Exception e)
            {
                Console.WriteLine("\nMain Exception raised!");
                Console.WriteLine("Source :{0} ", e.Source);
                Console.WriteLine("Message :{0} ", e.Message);
            }
        }