201710222120->利用httplistener实现简易服务器(未含逻辑及数据库)

目的:

利用请求的url参数来判断走哪一条协议

然后执行协议下指定的方法就可以勒

关键点:

1.httplistener获取请求

2.httplistener拿请求参数

3.反射Assembly,利用类名实例化字符串

详细代码:

--------------------------Program.cs--------------------------

    internal class Program
    {
        private static void Main(string[] args)
        {
            Processor processor = new Processor();
            processor.Listen();
        }
    }

--------------------------Processor.cs--------------------------

    internal class Processor
    {
        protected string Url = string.Empty;
        protected int Port = 0;
        protected HttpListener httpListener;
        protected bool IsRun = true;
        protected static readonly object MyLock = new object();

        private string mCurrentAction = string.Empty;
        private Type mCurrentType = null;
        private HttpListenerContext mCurrentContext = null;
        private object mCurrentObject = null;
        private MethodInfo mCurrentMethodInfo = null;
        private object[] mParams;

        public Processor()
        {
            this.Url = "http://localhost:8080/";
            this.Port = 8080;
        }

        public Processor(string url, int port)
        {
            this.Url = url;
            this.Port = port;
        }

        public void Listen()
        {
            httpListener = new HttpListener();
#if true

            httpListener.Prefixes.Add(Url);
#else

            httpListener.Prefixes.Add(string.Format(this.Url + ":" + this.Port + "/"));
#endif

            httpListener.Start();
            Console.WriteLine("服务器已经开启...");

            lock (MyLock)
            {
                while (IsRun)
                {
                    mCurrentContext = httpListener.GetContext();
                    if (mCurrentContext.GetActionId() != null && mCurrentContext.GetActionUserID() != null)
                    {
                        Console.WriteLine(mCurrentContext.Request.UserHostAddress + "访问" + mCurrentContext.GetActionId() + "...");
                        mCurrentAction = string.Format("Action{0}", mCurrentContext.GetActionId());
                        GetAction(mCurrentAction);
                    }
                    else
                    {
                        mCurrentContext.WriteFail();
                    }
                    mCurrentContext.Response.Close();
                }
            }
        }

        private void GetAction(string mCurrentAction)
        {
            mCurrentType = Type.GetType("MyHttpServer.Action." + mCurrentAction);
            if (mCurrentType.IsNonNull())
            {
                mParams = new object[] { mCurrentContext };
                this.mCurrentObject = Activator.CreateInstance(mCurrentType, mParams);
                if (this.mCurrentContext.Request.HttpMethod == "GET")
                {
                    mCurrentMethodInfo = mCurrentType.GetMethod("HandleGetRequest");
                    mCurrentMethodInfo.Invoke(this.mCurrentObject, null);
                }
                else if (this.mCurrentContext.Request.HttpMethod == "POST")
                {
                    mCurrentMethodInfo = mCurrentType.GetMethod("HandlePostRequest");
                    mCurrentMethodInfo.Invoke(this.mCurrentObject, null);
                }
            }
        }
    }

--------------------------Processor.cs---------------------------


--------------------------BaseAction.cs--------------------------

    internal abstract class BaseAction
    {
        protected HttpListenerContext mCurrentContext = null;
        protected StreamReader streamReader = null;
        protected StreamWriter streamWriter = null;

        private BaseAction()
        {
        }

        public BaseAction(HttpListenerContext CurrentContext)
        {
            this.mCurrentContext = CurrentContext;
        }

        public abstract void HandleGetRequest();

        public abstract void HandlePostRequest();
    }

--------------------------BaseAction.cs--------------------------


--------------------------Action10000.cs--------------------------

    internal class Action10000 : BaseAction
    {
        public Action10000(HttpListenerContext CurrentContext) : base(CurrentContext)
        {
        }

        public override void HandleGetRequest()
        {
            this.streamWriter = new StreamWriter(this.mCurrentContext.Response.OutputStream);
            this.streamWriter.Write("Hello World!!!");
            this.streamWriter.Close();
        }

        public override void HandlePostRequest()
        {
            throw new NotImplementedException();
        }
    }

--------------------------Action10000.cs--------------------------



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值