zoukankan      html  css  js  c++  java
  • 基于HttpListener的web服务器

     

     

    写在前面

    前面两篇文章分别介绍了基于原始socket的web服务器和基于tcpListener的web服务器,本篇文章将继续介绍另外一种基于HttpListener的。

    HttpListener

    HttpListener进一步的简化了Http协议的监听,仅需通过字符串的方法提供监听的地址和端口号以及虚拟路径,就可以开始监听工作了。

    复制代码
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace HttpListenerWebServer
    {
        class Program
        {
            static void Main(string[] args)
            {
                //httpListener提供一个简单,可通过编程方式控制的Http协议侦听器。此类不能被继承。
                if (!HttpListener.IsSupported)
                {
                    //该类只能在Windows xp sp2或者Windows server 200 以上的操作系统中才能使用,因为这个类必须使用Http.sys系统组件才能完成工作
                    //。所以在使用前应该先判断一下是否支持该类
                    Console.WriteLine("Windows xp sp2 or server 2003 is required to use the HttpListener class");
                }
                //设置前缀,必须以‘/’结尾
                string[] prefixes = new string[] { "http://localhost:8888/" };
                //初始化监听器
                HttpListener listener = new HttpListener();
                //将前缀添加到监听器
                foreach (var item in prefixes)
                {
                    listener.Prefixes.Add(item);
                }
                //判断是否已经启动了监听器,如果没有则开启
                if (!listener.IsListening)
                {
                    listener.Start();
                }
                //提示
                Console.WriteLine("服务器已经启动,开始监听....");
                while (true)
                {
                    //等待传入的请求,该方法将阻塞进程,直到收到请求
                    HttpListenerContext context = listener.GetContext();
                    //取得请求的对象
                    HttpListenerRequest request = context.Request;
                    //打印状态行 请求方法,url,协议版本
                    Console.WriteLine("{0} {1} HTTP/{2}\r\n", request.HttpMethod, request.RawUrl, request.ProtocolVersion);
                    //打印接收类型
                    Console.WriteLine("Accept: {0}", string.Join(",", request.AcceptTypes));
                    //打印接收语言
                    Console.WriteLine("Accept-Language: {0}", string.Join(",", request.UserLanguages));
                    //打印编码格式
                    Console.WriteLine("Accept-Encoding: {0}", string.Join(",", request.Headers["Accept-Encoding"]));
                    //客户端引擎
                    Console.WriteLine("User-Agent: {0}", string.Join(",", request.UserAgent));
                    //是否长连接
                    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 responseBody = "<html><head><title>这是一个web服务器的测试</title></head><body><h1>Hello World.</h1></body></html>";
                    //设置响应头部内容,长度及编码
                    response.ContentLength64 = System.Text.Encoding.UTF8.GetByteCount(responseBody);
                    response.ContentType = "text/html; Charset=UTF-8";
                    //输出响应内容
                    System.IO.Stream output = response.OutputStream;
                    System.IO.StreamWriter sw = new System.IO.StreamWriter(output);
                    sw.Write(responseBody);
                    sw.Dispose();
                    break;
                }
                //关闭服务器
                listener.Close();
                Console.Read();
    
            }
        }
    }
    复制代码

    启动服务器,并在浏览器中输入:http://localhost:8888/ 回车

    总结

    在使用httplistener时,我们可以通过对象的属性获取到请求和响应的参数。

    • 博客地址:http://www.cnblogs.com/wolf-sun/ 
      博客版权:如果文中有不妥或者错误的地方还望高手的你指出,以免误人子弟。如果觉得本文对你有所帮助不如【推荐】一下!如果你有更好的建议,不如留言一起讨论,共同进步! 再次感谢您耐心的读完本篇文章。
  • 相关阅读:
    A
    博弈论
    K
    快速幂
    基数排序
    计数排序
    KMP求字符串最小循环节
    二分图多重匹配
    hdu2818行列匹配+排序
    二分图行列匹配与最大匹配必须边
  • 原文地址:https://www.cnblogs.com/robertyao/p/8110156.html
Copyright © 2011-2022 走看看