C# HttpServer 框架

C# HttpServer 框架

Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Runtime.InteropServices;

namespace MySpace
{
    class Program
    {
        static void Main(string[] args)
        {
            HttpServer httpServer = new HttpServer(16130);
            httpServer.Start();

            // 隐藏窗口
            Console.Title = "DataProcessService";
            HideWindow();

            Thread.Sleep(System.Threading.Timeout.Infinite);
        }

        // 隐藏控制台
        private static void HideWindow()
        {
            ShowWindow(FindWindow(null, "DataProcessService"), 0);
        }

        [DllImport("User32.dll", EntryPoint = "FindWindow")]
        private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

        [DllImport("User32.dll", EntryPoint = "ShowWindow")] //
        private static extern bool ShowWindow(IntPtr hWnd, int type);
    }
}
HttpServer.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;

namespace MySpace
{
    public class HttpServer
    {
        private HttpListener _listener;
        public static int _port;

        public HttpServer(int port)
        {
            _port = port;
        }

        public void Start()
        {
            _listener = new HttpListener();
            _listener.Prefixes.Add(String.Format("http://*:{0}/", _port.ToString()));
            _listener.Start();
            _listener.BeginGetContext(ProcessRequest, null);
        }

        public void Stop()
        {
            _listener.Stop();
        }

        private void ProcessRequest(IAsyncResult result)
        {
            HttpListenerContext context = null;
            try
            {
                _listener.BeginGetContext(ProcessRequest, null);
                context = _listener.EndGetContext(result);

                // 发送短信内容
                var reader = new System.IO.StreamReader(context.Request.InputStream);
                String data = reader.ReadToEnd();

                // Http回复
                using (StreamWriter writer = new StreamWriter(context.Response.OutputStream))
                {
                    context.Response.StatusCode = 200;
                    writer.Write(data);
                    writer.Close();
                }
                context.Response.StatusCode = 200;
                context.Response.Close();
            }
            catch (Exception e)
            {
                using (StreamWriter writer = new StreamWriter(context.Response.OutputStream))
                {
                    context.Response.StatusCode = 400;
                    writer.Write(e.Message);
                    writer.Close();
                }
            }
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值