适用场景参考陈硕的《构建易于维护的分布式程序》
static void Main(string[] args)
{
//查看方式 http://127.0.0.1:8003/?name=TomAndJerry
using (HttpListener listerner = new HttpListener())
{
listerner.AuthenticationSchemes = AuthenticationSchemes.Anonymous; //指定身份验证 Anonymous匿名访问
listerner.Prefixes.Add("http://127.0.0.1:8003/");
listerner.Start();
while (true)
{
HttpListenerContext ctx = listerner.GetContext();
ctx.Response.StatusCode = 200;//设置返回给客服端http状态代码
using (StreamWriter writer = new StreamWriter(ctx.Response.OutputStream))
{
writer.WriteLine("<html><head><title>The WebServer Test</title></head><body>{0}</body></html>",ctx.Request.QueryString["name"]); //封装的时候此处做事件判断将ctx.Request作参数传递出去即可
writer.Close();
ctx.Response.Close();
}
}
}
}
{
//查看方式 http://127.0.0.1:8003/?name=TomAndJerry
using (HttpListener listerner = new HttpListener())
{
listerner.AuthenticationSchemes = AuthenticationSchemes.Anonymous; //指定身份验证 Anonymous匿名访问
listerner.Prefixes.Add("http://127.0.0.1:8003/");
listerner.Start();
while (true)
{
HttpListenerContext ctx = listerner.GetContext();
ctx.Response.StatusCode = 200;//设置返回给客服端http状态代码
using (StreamWriter writer = new StreamWriter(ctx.Response.OutputStream))
{
writer.WriteLine("<html><head><title>The WebServer Test</title></head><body>{0}</body></html>",ctx.Request.QueryString["name"]); //封装的时候此处做事件判断将ctx.Request作参数传递出去即可
writer.Close();
ctx.Response.Close();
}
}
}
}