当前位置:编程学习 > asp >>

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

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;
&nbs

补充:Web开发 , ASP.Net ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,