Question

private void ListenerCallback(IAsyncResult ar)
        {
            _busy.WaitOne();
            try
            {
                HttpListenerContext context;
                try
                { context = _listener.EndGetContext(ar); }
                catch (HttpListenerException)
                { return; }

                if (_stop.WaitOne(0, false))
                    return;

                var sr = new StreamReader(context.Request.InputStream);
                string x = sr.ReadToEnd();
                Console.WriteLine("{0} {1}", context.Request.HttpMethod, x);
                //context.Response.SendChunked = true;
                using (TextWriter tw = new StreamWriter(context.Response.OutputStream))
                {
                    //for (int i = 0; i < 5; i++)
                    {
                        //tw.WriteLine("<p>{0} @ {1}</p>", i, DateTime.Now);
                        tw.WriteLine("<html><head></head><body>");
                        tw.WriteLine("Server Response");
                        tw.WriteLine("</body></html>");
                        tw.Flush(); //Catch http exception if client exists halfway through
                        //Thread.Sleep(1000);
                    }
                }
            }
            finally
            {
                if (_maxThreads == 1 + _busy.Release())
                    _idle.Set();
            }
        }

Above is my code, I can go to the URL with Chrome and few the reply even though the server shows it takes 2 get requests, I want to be able to handle POST requests, when I send a post request it reads it properly but the client doesn't get the reply.

Was it helpful?

Solution

You should add ctx.Response.ContentLength64=.... (you may also need ctx.Response.Close())

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top