简单基于HttpListener的Web服务器实例

来源:未知 责任编辑:智问网络 发表时间:2013-11-04 19:50 点击:

1.创建一个控制台工程,输入如下代码

static void Main(string[] args) 

    if(!HttpListener.IsSupported) 
    { 
        throw new System.InvalidOperationException("版本太低"); 
    } 
    string[] prefixes = new string[] { "http://localhost:50000/" }; 
    HttpListener listener = new HttpListener(); 
    // 添加前缀  
    foreach(string s in prefixes) 
    { 
        listener.Prefixes.Add(s); 
    } 
    // 开始监听  
    listener.Start(); 
    Console.WriteLine("监听中、、"); 
    while (true) 
    { 
        HttpListenerContext context = listener.GetContext(); 
        // 取得请求对象  
        HttpListenerRequest request = context.Request; 
        Console.WriteLine("{0}   {1}   HTTP/1.1", request.HttpMethod, request.RawUrl); 
        Console.WriteLine("Accept:  {0}", string.Join(",", request.AcceptTypes)); 
        Console.WriteLine("Accept-Language:  {0}", string.Join(",", request.UserLanguages)); 
        Console.WriteLine("User-Agent:  {0}", request.UserAgent); 
        Console.WriteLine("Accept-Encoding:   {0}", request.Headers["Accept-Encoding"]); 
        Console.WriteLine("Connection:  {0}", request.KeepAlive ? "Keep-Alive" : "close"); 
        Console.WriteLine("Host:  {0}", request.UserHostName); 
        Console.WriteLine("Pragma: {0}", request.Headers["Pragma"]); 
 
        //获取回应对象  
        HttpListenerResponse response = context.Response; 
        string responseString = "<html><head><title>response server</title></head><body>hello world!</body></html>"; 
        response.ContentLength64 = System.Text.Encoding.UTF8.GetByteCount(responseString); 
        response.ContentType = "text/html; charset=UTF-8";        //设置输出类型  
        System.IO.Stream output = response.OutputStream; 
        System.IO.StreamWriter writer = new System.IO.StreamWriter(output); 
        writer.Write(responseString); 
 
        writer.Close(); 
        if (Console.KeyAvailable) 
        { 
            break; 
        } 
    } 
    listener.Stop(); 
 

        static void Main(string[] args)
        {
            if(!HttpListener.IsSupported)
            {
                throw new System.InvalidOperationException("版本太低");
            }
            string[] prefixes = new string[] { "http://localhost:50000/" };
            HttpListener listener = new HttpListener();
            // 添加前缀
            foreach(string s in prefixes)
            {
                listener.Prefixes.Add(s);
            }
            // 开始监听
            listener.Start();
            Console.WriteLine("监听中、、");
            while (true)
            {
                HttpListenerContext context = listener.GetContext();
                // 取得请求对象
                HttpListenerRequest request = context.Request;
                Console.WriteLine("{0}   {1}   HTTP/1.1", request.HttpMethod, request.RawUrl);
                Console.WriteLine("Accept:  {0}", string.Join(",", request.AcceptTypes));
                Console.WriteLine("Accept-Language:  {0}", string.Join(",", request.UserLanguages));
                Console.WriteLine("User-Agent:  {0}", request.UserAgent);
                Console.WriteLine("Accept-Encoding:   {0}", request.Headers["Accept-Encoding"]);
                Console.WriteLine("Connection:  {0}", request.KeepAlive ? "Keep-Alive" : "close");
                Console.WriteLine("Host:  {0}", request.UserHostName);
                Console.WriteLine("Pragma: {0}", request.Headers["Pragma"]);

                //获取回应对象
                HttpListenerResponse response = context.Response;
                string responseString = "<html><head><title>response server</title></head><body>hello world!</body></html>";
                response.ContentLength64 = System.Text.Encoding.UTF8.GetByteCount(responseString);
                response.ContentType = "text/html; charset=UTF-8";        //设置输出类型
                System.IO.Stream output = response.OutputStream;
                System.IO.StreamWriter writer = new System.IO.StreamWriter(output);
                writer.Write(responseString);

                writer.Close();
                if (Console.KeyAvailable)
                {
                    break;
                }
            }
            listener.Stop();

        }
2.运行工程

3.效果如图

 \

  摘自 hi_dzj的专栏

    发表评论
    请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
    用户名: 验证码:点击我更换图片
    最新评论 更多>>

    推荐热点

    • 浅析.NET下XML数据访问新机制
    • asp.net 面试+笔试题目第1/2页
    • C# 邮件地址是否合法的验证
    • C#高级编程:数据库连接[1]
    • asp.net 设置GridView的选中行的实现代码
    • 经典C++程序1
    • IIS 自动回收导致后台定时器失效的问题解决
    • ASP.NET&#160;GridView列表代码示例
    • 微软ASP.NET站点部署指南(3):使用Web.Config文件的Transforma
    网站首页 - 友情链接 - 网站地图 - TAG标签 - RSS订阅 - 内容搜索
    Copyright © 2008-2015 计算机技术学习交流网. 版权所有

    豫ICP备11007008号-1