HTTP listener 的封装---HttpListenerEx

HttpListener用途:监听HTTP请求。


示例:


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


namespace HTTPListenerDemo
{
    public class HttpListenerEx
    {


        #region Members


        /// <summary>
        /// 当前监听HTTP请求的对象
        /// </summary>
        private HttpListener currentListener;


        /// <summary>
        /// 停止监听所有请求
        /// </summary>
        private bool StopAll = false;


        #endregion


        #region Constructor


        /// <summary>
        /// 初始化时指定一个要监听的网络地址,以'/'结尾
        /// 例如:"http://contoso.com:8080/index/".
        /// </summary>
        /// <param name="url">要监听的网络地址</param>
        public HttpListenerEx(string url)
        {
            this.currentListener = new HttpListener();
            this.currentListener.Prefixes.Add(url);
        }


        /// <summary>
        /// 初始化时指定多个地址,同时监听,地址以'/'结束
        /// 例如:"http://contoso.com:8080/index/".
        /// </summary>
        /// <param name="urls">要监听的网络地址集合</param>
        public HttpListenerEx(string[] urls)
        {
            this.currentListener = new HttpListener();
            foreach (string s in urls)
            {
                this.currentListener.Prefixes.Add(s);
            }
        }


        #endregion


        #region Methods


        /// <summary>
        /// 监听HTTP请求(只处理第一次的请求,处理完之后就结束了)
        /// </summary>
       /// <param name="OnGetRequestStr">将接收到的请求字符串交给OnGetRequestStr处理</param>
        public void ListenForOnce(string responseStr, Action<string> OnRequestGet)
        {
            if (!HttpListener.IsSupported)
            {
                // Console.WriteLine("Windows XP SP2 or Server 2003 is required to use the HttpListener class.");
                return;
            }


            ////开始监听
            this.currentListener.Start();


            ////处理请求
            HandleRequest(responseStr, OnRequestGet);


            ////停止监听
            this.currentListener.Stop();
        }


        /// <summary>
        /// 监听HTTP请求,监听到之后,立刻返给客户端一个字符串
        /// </summary>
       /// <param name="OnGetRequestStr">将接收到的请求字符串交给OnGetRequestStr处理</param>
        public void StartListen(string responseStr, Action<string> OnGetRequest)
        {
            if (!HttpListener.IsSupported)
            {
                // Console.WriteLine("Windows XP SP2 or Server 2003 is required to use the HttpListener class.");
                return;
            }
            ////开始监听
            this.currentListener.Start();


            ////处理请求
            while (!StopAll)
            {
                HandleRequest(responseStr, OnGetRequest);
            }
        }


        /// <summary>
        /// 结束监听
        /// </summary>
        public void EndListen()
        {
            StopAll = true;
            this.currentListener.Abort();
        }


        #endregion


        #region Private Methods


        /// <summary>
        /// 处理请求
        /// </summary>
        /// <param name="OnGetRequestStr">将接收到的请求字符串交给OnGetRequestStr处理</param>
        private void HandleRequest(string responseStr, Action<string> OnGetRequestStr)
        {
            HttpListenerContext context = this.currentListener.GetContext();
            HttpListenerRequest request = context.Request;
            // Obtain a response object.
            HttpListenerResponse response = context.Response;
            StreamReader sr = new StreamReader(request.InputStream);
            OnGetRequestStr(sr.ReadToEnd());
            // Construct a response.
            byte[] buffer = System.Text.Encoding.UTF8.GetBytes(responseStr);
            // 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();
        }


        #endregion
    }
}


你可能感兴趣的:(String,buffer,Constructor,output,methods)