簡體   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