目的:
利用请求的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--------------------------