zoukankan      html  css  js  c++  java
  • 程序中内嵌Http服务的实现原型

    适用场景参考陈硕的《构建易于维护的分布式程序
    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();
                        }
                    }
                }
            }
  • 相关阅读:
    算法:最大回文子串问题
    Python HTTP库requests中文页面乱码解决方案!
    Python:默认参数
    在博客园开通个人博客了
    JQ选择器
    C# 接口
    C# 微信公众平台开发(2) 微信菜单
    C# 微信公众平台开发(1) 服务器配置
    博客园开博第一天!记录生活,扬帆启航!
    JavaScript判断、循环、Map、Set
  • 原文地址:https://www.cnblogs.com/jiang_zheng/p/InnerHttpService.html
Copyright © 2011-2022 走看看