c#编写一个简单的http服务器

先来张我的帅照哈哈哈

 

好了不臭美了   上代码

世间万物 只有想不到 没有做不到  哈哈哈  仔细阅读代码     我要凑够 150个字  哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Net;
 6 using System.IO;
 7 
 8 namespace ConsoleApplication1
 9 {
10     class Program
11     {
12         static void Main(string[] args)
13         {
14 
15             using (HttpListener listerner = new HttpListener())
16             {
17                 listerner.AuthenticationSchemes = AuthenticationSchemes.Anonymous;//指定身份验证 Anonymous匿名访问
18                 listerner.Prefixes.Add("http://localhost:8080/web/");
19                 listerner.Start();
20                 Console.WriteLine("WebServer Start Successed.......");
21                 while (true)
22                 {
23                     //等待请求连接
24                     //没有请求则GetContext处于阻塞状态
25                     HttpListenerContext ctx = listerner.GetContext();
26                     ctx.Response.StatusCode = 200;//设置返回给客服端http状态代码
27                     string name = ctx.Request.QueryString["name"];
28 
29                     if (name != null)
30                     {
31                         Console.WriteLine(name);
32                     }
33 
34 
35                     //使用Writer输出http响应代码
36                     using (StreamWriter writer = new StreamWriter(ctx.Response.OutputStream))
37                     {
38                         Console.WriteLine("hello");
39                         writer.WriteLine("<html><head><title>The WebServer Test</title></head><body>");
40                         writer.WriteLine("<div style=\"height:20px;color:blue;text-align:center;\"><p> hello {0}</p></div>", name);
41                         writer.WriteLine("<ul>");
42 
43                         foreach (string header in ctx.Request.Headers.Keys)
44                         {
45                             writer.WriteLine("<li><b>{0}:</b>{1}</li>", header, ctx.Request.Headers[header]);
46 
47                         }
48                         writer.WriteLine("</ul>");
49                         writer.WriteLine("</body></html>");
50 
51                         writer.Close();
52                         ctx.Response.Close();
53                     }
54 
55                 }
56                 listerner.Stop();
57             }
58         }
59 
60  
61 
62     }
63 }

简不简单 明不明了  哈哈哈  低调 

 

转载于:https://www.cnblogs.com/-jth/p/10639589.html

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值