基于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>

 

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

总结

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