您的位置:首页 > 理论基础 > 计算机网络

程序中内嵌Http服务的实现原型

2011-05-12 09:41 417 查看
适用场景参考陈硕的《构建易于维护的分布式程序》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();
}
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: