如何使用C#处理流水线请求?

时间:2015-05-04 20:02:15

标签: c# .net mono pipelining

我需要在我的C#Mono(Linux上的Mono版本3.12.1)服务器上处理流水线请求,但我注意到管道中的第二个请求总是被忽略。

这是我测试流水线的netcat命令:

nc -v localhost 5000 < httppipe.txt 

这是httppipe.txt的内容:

GET /heartbeat HTTP/1.1
Host: localhost

GET /heartbeat HTTP/1.1
Host: localhost

我非常自信我的netcat方法有效,因为我在Java服务器上成功测试了它。 (意思是我看到了2条回复)

在我的C#服务器中,我尝试了GetResult和GetResultAsync。具有GetResultAsync的代码基本上是从MSDN示例中直接引出的。它看起来像这样:

this.Listener = new HttpListener();
this.Listener.Prefixes.Add(uriPrefix);
this.Listener.BeginGetContext(new AsyncCallback(ListenerCallback),this.Listener);

public static void ListenerCallback(IAsyncResult result)
        {
            HttpListener listener = (HttpListener) result.AsyncState;
            // Call EndGetContext to complete the asynchronous operation.
            HttpListenerContext context = listener.EndGetContext(result);
            listener.BeginGetContext(new AsyncCallback(ListenerCallback), listener);
            HttpListenerRequest request = context.Request;
            // Obtain a response object.
            HttpListenerResponse response = context.Response;
            // Construct a response. 
            string responseString = "<HTML><BODY> Hello world!</BODY></HTML>";
            byte[] buffer = System.Text.Encoding.UTF8.GetBytes(responseString);
            // Get a response stream and write the response to it.
            response.ContentLength64 = buffer.Length;
            System.IO.Stream output = response.OutputStream;
            output.Write(buffer,0,buffer.Length);
            // You must close the output stream.
            output.Close(); 
        }

编辑:在Linux上的Mono 4.0上也尝试过无济于事。

0 个答案:

没有答案