zoukankan      html  css  js  c++  java
  • C#监听服务

    控制台应用程序

     1 class Program
     2     {
     3         static void Main(string[] args)
     4         {
     5             HttpListenerService hls = new HttpListenerService();
     6             hls.OnStart(null);
     7             Console.WriteLine("按任意键退出...");
     8             Console.ReadLine();       
     9         }
    10     }

    监听服务

     1 internal class HttpListenerService
     2     {
     3         /// <summary>
     4         /// 构造函数
     5         /// </summary>
     6         public HttpListenerService() { }
     7 
     8         public void OnStart(string[] args)
     9         {
    10             Console.Title = "HTTP 监听服务!";
    11             //http listener
    12             System.Net.HttpListener httplistener = new System.Net.HttpListener();
    13             httplistener.Prefixes.Add("http://localhost:8855/data/sync/");
    14             httplistener.Prefixes.Add("http://localhost:8855/data/listen/");
    15             //httplistener.ExtendedProtectionSelectorDelegate += (o) =>
    16             //    {
    17             //        o.Headers["Server"] = "DTK-Server";
    18             //        return null;
    19             //    };
    20             try
    21             {
    22                 httplistener.Start();
    23                 //Handle(httplistener);
    24                 System.Threading.ThreadPool.SetMinThreads(10, 3);
    25        
    26                 System.Threading.ThreadPool.SetMaxThreads(1000, 512);
    27 
    28                 //System.Threading.ThreadPool.QueueUserWorkItem((state) => { Handle((System.Net.HttpListener)state); }, httplistener);
    29                 System.Threading.ThreadPool.QueueUserWorkItem((start) => { Handle(httplistener); });
    30 
    31                 FluentConsole.Green.Line("API监听服务启动成功!");
    32             }
    33             catch (Exception ex)
    34             {
    35                 FluentConsole.Red.Line(ex.Message);
    36                 if (httplistener.IsListening)
    37                 {
    38                     httplistener.Stop();
    39                 }
    40             }
    41         }
    42 
    43         private void Handle(System.Net.HttpListener httpListener)
    44         {
    45             while (true)
    46             {
    47                 System.Net.HttpListenerContext context = httpListener.GetContext();
    48                 System.Net.HttpListenerRequest request = context.Request;
    49                 //context.Response.Headers["Server"] = "DTK-Server";
    50                 if (httpListener.IsListening && request != null)
    51                 {
    52                     System.Net.HttpListenerResponse response = context.Response;
    53                     
    54                     if (request.RawUrl.IndexOf("/data/listen/") > -1)
    55                     {
    56                         for (int i = 0; i < 10; i++)
    57                         {
    58                             FluentConsole.Green.Line("请求成功>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
    59                             FluentConsole.Green.Line("-------------------------------<>---------------------------");
    60                         }
    61                     }
    62                 }
    63             }
    64         }
    65     }
    HttpListenerService
  • 相关阅读:
    在AE中通过SDE添加图层(转)
    上一视图下一视图功能实现(C#+SuperMap Objects)
    [Python入门及进阶笔记]Python基础内置函数小结
    Javascript小球
    64位centos下安装python的PIL模块
    [Python入门及进阶笔记]Python基础集合小结
    C语言的那些小秘密之【链表(二)】
    巴斯卡三角形
    [Python入门及进阶笔记]Python基础数字处理相关模块
    C语言的那些小秘密之【链表(一)】
  • 原文地址:https://www.cnblogs.com/shiyige-216/p/7813665.html
Copyright © 2011-2022 走看看