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
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值