C#怎么实现一个简单的Web服务器 C# HttpListener使用方法

来源:这里教程网 时间:2026-02-21 17:33:22 作者:

用 C# 实现一个简单的 Web 服务器,

HttpListener
是最轻量、无需 IIS 或 Kestrel 的原生方案,适合调试、内网工具或嵌入式场景。它不依赖 ASP.NET Core,.NET Framework 和 .NET 5+ 都支持(注意权限和前缀注册)。

基础用法:监听并响应一个 GET 请求

核心步骤就三步:创建监听器、添加 URL 前缀、启动监听并处理上下文。

必须以管理员权限运行(Windows 上注册 http://localhost:8080/ 等前缀需要) 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");
<p>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();
}

支持多种 HTTP 方法和路径路由

ctx.Request.HttpMethod
ctx.Request.Url.LocalPath
可做简单路由判断。

区分 GET /api/data 和 POST /submit,用 if 判断即可 读取 POST 数据:用
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()
分发上下文,避免阻塞主线程

注册 URL 前缀(Windows 必做)

首次运行常报错“拒绝访问”,是因为系统没授权该用户监听该地址。打开管理员命令行执行:

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 路由。不需要重造轮子时,它足够快、够轻、够直接。

相关推荐