HttpListener对象实现HTTP监听服务

原创
2017/01/20 16:57
阅读数 9.3K

本文以System.Net.HttpListener类为例,演示何如创建一个HTTP服务。可以在此基础上,进行扩展,也可将该程序制作成一个Service服务。

        /// <summary>
        /// <![CDATA[主程序入口]]>
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            //http listener
            System.Net.HttpListener httpListener = new HttpListener();
            httpListener.Prefixes.Add("http://172.16.16.65/TiKuSync/");//
            httpListener.Start();
            try
            {
                System.Threading.ThreadPool.SetMinThreads(8, 5);
                System.Threading.ThreadPool.SetMaxThreads(100, 30);
                while (true)
                {
                    try
                    {
                        HttpListenerContext context = httpListener.GetContext();
                        System.Threading.ThreadPool.QueueUserWorkItem((o) => { Handler(context); });
                    }
                    catch (HttpListenerException exception)
                    {
                        Console.WriteLine(exception.Message);
                        break;
                    }
                    catch (InvalidOperationException exception)
                    {
                        Console.WriteLine(exception.Message);
                        break;
                    }
                }
            }
            catch (Exception ex)
            {
                //if (httpListener.IsListening) { httpListener.Stop(); }
                Console.WriteLine(ex.Message);
            }
            Console.ReadLine();
        }

新建一个控制台项目,在Main函数里,编写HTTP监听的实现代码。

为监听请求指定处理函数:

        /// <summary>
        //<![CDATA[处理程序]]>
        /// </summary>
        private static void Handler(HttpListenerContext context)
        {

            try
            {
                Counter++;
                HttpListenerRequest request = context.Request;
                HttpListenerResponse response = context.Response;

                string responseString = "<html><head><meta http-equiv=Content-Type content=\"text/html;charset=utf-8\"></head><body>This is HttpListener Service!请求: " + Counter + "</body></html>";
                byte[] buffer = System.Text.Encoding.UTF8.GetBytes(responseString);
                response.ContentEncoding = Encoding.UTF8;
                response.ContentLength64 = buffer.Length;
                response.KeepAlive = true;
                using (System.IO.Stream output = response.OutputStream)
                {
                    output.Write(buffer, 0, buffer.Length);
                    output.Flush();
                }
                response.Close();

            }
            catch (Exception exception)
            {
                Console.WriteLine(exception.Message);
            }
            finally
            {
                Console.WriteLine("请求次数:{0}", Counter);
            }
        }

效果如下:

展开阅读全文
加载中

作者的其它热门文章

打赏
0
0 收藏
分享
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部