繁体   English   中英

在HttpListener中处理发布请求

[英]Handling post requests in HttpListener

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();
            }
        }

上面是我的代码,即使服务器显示它需要2个get请求,我也可以使用Chrome浏览器访问URL,并且很少回复,我希望能够处理POST请求,当我发送发布请求时,它可以正确读取,但客户未收到回复。

您应该添加ctx.Response.ContentLength64=... (您可能还需要ctx.Response.Close()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM