C# 在HttpListener中处理post请求 private void ListenerCallback(IAsyncResult ar) { _忙。等一等(); 尝试 { HttpListenerContext; 尝试 {context=_listener.EndGetContext(ar);} 捕获(HttpListenerException) {return;} if(_stop.WaitOne(0,false)) 返回; var sr=新的StreamReader(context.Request.InputStream); 字符串x=sr.ReadToEnd(); WriteLine(“{0}{1}”,context.Request.HttpMethod,x); //context.Response.SendChunked=true; 使用(TextWriter tw=newstreamWriter(context.Response.OutputStream)) { //对于(int i=0;i

C# 在HttpListener中处理post请求 private void ListenerCallback(IAsyncResult ar) { _忙。等一等(); 尝试 { HttpListenerContext; 尝试 {context=_listener.EndGetContext(ar);} 捕获(HttpListenerException) {return;} if(_stop.WaitOne(0,false)) 返回; var sr=新的StreamReader(context.Request.InputStream); 字符串x=sr.ReadToEnd(); WriteLine(“{0}{1}”,context.Request.HttpMethod,x); //context.Response.SendChunked=true; 使用(TextWriter tw=newstreamWriter(context.Response.OutputStream)) { //对于(int i=0;i,c#,javascript,httplistener,C#,Javascript,Httplistener,{0}@{1}”,i,DateTime.Now); tw.WriteLine(“”); tw.WriteLine(“服务器响应”); tw.WriteLine(“”); tw.Flush();//如果客户端中途存在,则捕获http异常 //睡眠(1000); } } } 最后 { 如果(_maxThreads==1+_busy.Release()) _idle.Set(); } } 以上是我的代码,我可以使用Chrome浏览器访问URL,很少回复,即使服务器显示它需要2个get请求,我希望能够

{0}@{1}

”,i,DateTime.Now); tw.WriteLine(“”); tw.WriteLine(“服务器响应”); tw.WriteLine(“”); tw.Flush();//如果客户端中途存在,则捕获http异常 //睡眠(1000); } } } 最后 { 如果(_maxThreads==1+_busy.Release()) _idle.Set(); } }
以上是我的代码,我可以使用Chrome浏览器访问URL,很少回复,即使服务器显示它需要2个get请求,我希望能够处理POST请求,当我发送POST请求时,它会正确读取请求,但客户端没有收到回复。

您应该添加
ctx.Response.ContentLength64=…

(当我使用'byte[]buffer=System.Text.Encoding.UTF8.GetBytes(responseString)时,您可能还需要
ctx.Response.Close()

;context.Response.ContentLength64=buffer.Length;'我得到一个“要写入流的字节超过内容长度…”error@Max0999同时将缓冲区写入流,而不是原始字符串(并且您不再需要TextWriter。)表示“此流不支持搜索操作。”当我使用“context.Response.ContentLength64=tw.BaseStream.Length;”哈哈,很抱歉是个新手,只需使用
context.Response.OutputStream.Write(buffer,0,buffer.Length)
<不需要代码>搜索。我使用'Byte[]buffer=System.Text.Encoding.ASCII.GetBytes(responseString);context.Response.ContentLength64=buffer.Length;context.Response.OutputStream.Write(buffer,0,buffer.Length);context.Response.Close();'现在,每个响应仍然被分解为多个get请求。。提前谢谢
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();
            }
        }