测试HttpListener监听请求

测试HttpListener监听请求,代码如下:

#region 测试HttpListener

        /// <summary>
        /// 测试HttpListener
        /// </summary>
        public static async void TestHttpListener()
        {           
            var threadStart = new Thread(new ThreadStart(StartHttpListener))
            {
                IsBackground = true
            };
            threadStart.Start();
            await Task.Delay(TimeSpan.FromSeconds(3));
            Console.WriteLine($"开始测试数据");
            int test = 1;
            while (true)
            {
                if (test % 2 == 0)
                {
                    HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create($"http://localhost:5566?a={test}");
                    httpWebRequest.Method = "Get";
                    using var response = httpWebRequest.GetResponse().GetResponseStream();
                    var result = new StreamReader(response).ReadToEnd();
                    Console.WriteLine($"偶数--结果数据{result}");
                }
                else
                {
                    HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create($"http://localhost:5566?a={test}");
                    httpWebRequest.Method = "Post";
                    var bytes = Encoding.UTF8.GetBytes($"Post数据:{test}");
                    using var requestStream = httpWebRequest.GetRequestStream();
                    requestStream.Write(bytes, 0, bytes.Length);
                    requestStream.Flush();
                    using var response = httpWebRequest.GetResponse().GetResponseStream();
                    var result = new StreamReader(response).ReadToEnd();
                    Console.WriteLine($"奇数--结果数据{result}");
                }
                test++;
                Thread.Sleep(TimeSpan.FromSeconds(2));
            }
        }

        public static async void StartHttpListener()
        {
            await Task.Delay(TimeSpan.FromSeconds(3));
            HttpListener httpListener = new HttpListener();
            httpListener.Prefixes.Add("http://localhost:5566/");
            //httpListener.Prefixes.Add("http://+:5566/");
            //httpListener.Prefixes.Add("http://*:5566/");
            httpListener.Start();
            Console.WriteLine($"httpListener 启动监听");
            while (true)
            {
                var ssyncResult = httpListener.BeginGetContext(new AsyncCallback(Callback), httpListener);
                ssyncResult.AsyncWaitHandle.WaitOne();
            }
        }

        private static void Callback(IAsyncResult ar)
        {
            var httplist = (HttpListener)ar.AsyncState;
            var httpcontent = httplist.EndGetContext(ar);
            if (httpcontent.Request.HttpMethod == HttpMethod.Post.ToString())
            {
                Console.WriteLine($"请求方法:{httpcontent.Request.HttpMethod}");
                var input = new StreamReader(httpcontent.Request.InputStream).ReadToEnd();
                var bytes = Encoding.UTF8.GetBytes($"我们收到数据:{input},哈哈哈哈");
                httpcontent.Response.OutputStream.Write(bytes, 0, bytes.Length);
                httpcontent.Response.Close();
            }
            else
            {
                Console.WriteLine($"请求方法:{httpcontent.Request.HttpMethod}");
                var bytes = Encoding.UTF8.GetBytes($"{httpcontent.Request.RawUrl}----哈哈哈哈");
                httpcontent.Response.OutputStream.Write(bytes, 0, bytes.Length);
                httpcontent.Response.Close();
            }
        }

        #endregion

测试结果如下:

 public class Program
    {
        static void Main(string[] args)
        {
            #region 测试TestHttpListener
            TestHttpListener();
            #endregion
            Console.Read();
        }
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值