0

我基本上在这里运行示例:http: //msdn.microsoft.com/en-us/library/system.net.httplistener.begingetcontext.aspx

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;

namespace Servertest
{
    class Program
    {
        static string msg;
        public static void Main(string[] prefixes)
        {
            if (!HttpListener.IsSupported)
            {
                Console.WriteLine("Windows XP SP2 or Server 2003 is required to use the HttpListener class.");
                Console.ReadLine();
                return;
            }
            // URI prefixes are required,
            // for example "http://contoso.com:8080/index/".

            //if (prefixes == null || prefixes.Length == 0)
              //  throw new ArgumentException("prefixes");

            // Create a listener.
            HttpListener listener = new HttpListener();
            // Add the prefixes.
            //foreach (string s in prefixes)
            //{
            listener.Prefixes.Add("http://*:2999/");
            //}
            listener.Start();
            Console.WriteLine("Listening...");
            // Note: The GetContext method blocks while waiting for a request.
            IAsyncResult result = listener.BeginGetContext(new AsyncCallback(ListenerCallback), listener);
            // Applications can do some work here while waiting for the 
            // request. If no work can be done until you have processed a request,
            // use a wait handle to prevent this thread from terminating
            // while the asynchronous operation completes.
            Console.WriteLine("Waiting for request to be processed asyncronously.");
            result.AsyncWaitHandle.WaitOne();
            Console.WriteLine("Request processed asyncronously.");
            Console.WriteLine(msg);
            listener.Close();
        }
        public static void ListenerCallback(IAsyncResult result)
        {
            HttpListener listener = (HttpListener)result.AsyncState;
            // Call EndGetContext to complete the asynchronous operation.
            HttpListenerContext context = listener.EndGetContext(result);
            HttpListenerRequest request = context.Request;
            // Obtain a response object.
            //Break point here doesnt even get stopped
            //Console.WriteLine(request.RawUrl) doesn't show anything either
            msg = new string(request.RawUrl.ToCharArray());
            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();

        }
    }
}

RawUrl 没有被打印出来,而且 msg 也是空白的。我在回调函数中放置的断点甚至不会停止执行

4

1 回答 1

3

明白了,问题是 listener.Close() 是当 WaitOne() 返回时正在执行的主要函数,所以在 ListenerCallback 中它说资源已经被处理掉了。

在回调结束时也需要这个 IAsyncResult result = listener.BeginGetContext(new AsyncCallback(ListenerCallback), listener);

接受下一个请求

于 2012-08-09T01:47:36.530 回答