HttpListener 是 C# 原生轻量 Web 服务器方案,无需 IIS 或 Kestrel,支持 .NET Framework 和 .NET 5+;需管理员权限注册 URL 前缀(如 http://localhost:8080/),核心步骤为创建监听器、添加前缀、调用 GetContext() 或 GetContextAsync() 处理请求,注意异常捕获、资源释放与并发处理。
用 C# 实现一个简单的 Web 服务器,HttpListener 是最轻量、无需 IIS 或 Kestrel 的原生方案,适合调试、内网工具或嵌入式场景。它不依赖 ASP.NET Core,.NET Framework 和 .NET 5+ 都支持(注意权限和前缀注册)。
核心步骤就三步:创建监听器、添加 URL 前缀、启动监听并处理上下文。
"http://localhost:8080/",末尾斜杠不能省GetContext() 是阻塞的,收到请求才返回;可用 GetContextAsync() 配合 async/await示例代码:
var listener = new HttpListener();
listener.Prefixes.Add("http://localhost:8080/");
listener.Start();
Console.WriteLine("服务器已启动,访问 http://localhost:8080");
while (true)
{
var ctx = listener.GetContext(); // 阻塞等待请求
var response = ctx.Response;
var buffer = Encoding.UTF8.GetBytes("Hello from HttpListener!");
response.ContentLength64 = buffer.Length;
response.OutputStream.Write(buffer, 0, buffer.Length);
response.Close();
}
ctx.Request.HttpMethod 和 ctx.Request.Url.LocalPath 可做简单路由判断。
ctx.Request.InputStream + StreamReader(注意编码)response.StatusCode = 404;,响应头:response.Headers["Content-Type"] = "application/json";
例如返回 JSON:
if (ctx.Request.HttpMethod == "GET" && ctx.Request.Url.LocalPath == "/api/time")
{
var json = $"{{\"time\":\"{DateTime.Now:O}\"}}";
var buffer = Encoding.UTF8.GetBytes(json);
response.ContentType = "application/json";
response.ContentLength64 = buffer.Length;
response.OutputStream.Write(buffer, 0, buffer.Length);
}实际使用不能只写 while(true),要注意异常、资源释放和并发。
GetContext() 放在 try/catch 中,避免一次异常导致服务中断
监听器停止时调用 listener.Stop() 和 .Close()
response.Close(),否则连接可能挂起Task.Run() 分发上下文,避免阻塞主线程首次运行常报错“拒绝访问”,是因为系统没授权该用户监听该地址。打开管理员命令行执行:
netsh http add urlacl url=http://localhost:8080/ user=Everyone // 或指定当前用户名(推荐) netsh http add urlacl url=http://+:8080/ user="%USERDOMAIN%\%USERNAME%"
撤销命令是 netsh http delete urlacl url=http://localhost:8080/。用 + 表示绑定所有网卡(注意防火墙)。
基本上就这些。HttpListener 不复杂但容易忽略权限和资源释放,跑通后可封装成小工具类,比如带日志、静态文件服务或简单 API 路由。不需要重造轮子时,它足够快、够轻、够直接。