答案:使用C#的HttpListener类可创建轻量级Web服务器。首先配置监听前缀并注册URL权限,然后启动监听并处理请求,返回HTML响应,最后通过netsh命令管理端口访问权限。

在C#中实现一个简单的Web服务器,可以使用.NET Framework自带的 HttpListener 类。它不需要依赖IIS,能够监听指定的HTTP请求并返回响应内容,非常适合开发轻量级本地服务或调试用途。
要使用 HttpListener,首先需要创建实例,添加监听的URL前缀,并启动监听器。
注意:需要管理员权限运行程序,且URL前缀需通过 netsh 命令注册(Windows系统)。
示例代码:
using System;
using System.Net;
<p>class SimpleWebServer
{
private HttpListener _listener;</p><pre class="brush:php;toolbar:false;"><pre class="brush:php;toolbar:false;">public void Start(string prefix)
{
if (!HttpListener.IsSupported)
{
Console.WriteLine("当前系统不支持 HttpListener");
return;
}
_listener = new HttpListener();
_listener.Prefixes.Add(prefix); // 例如:https://www.php.cn/link/cbb686245ece57c9827c4bc0d0654a8e/
_listener.Start();
Console.WriteLine($"服务器已启动,监听地址:{prefix}");
Listen();
}
private async void Listen()
{
while (_listener.IsListening)
{
var context = await _listener.GetContextAsync();
HandleRequest(context);
}
}}
每个请求由 HttpListenerContext 表示,包含请求(HttpRequest)和响应(HttpResponse)对象。
你可以读取请求方法、路径、查询参数等,并写入响应内容。
示例处理逻辑:
private void HandleRequest(HttpListenerContext context)
{
var request = context.Request;
var response = context.Response;
<pre class="brush:php;toolbar:false;"><pre class="brush:php;toolbar:false;">string responseString = $@"
<html>
<body>
<h2>Hello from C# Web Server!</h2>
<p>请求路径:{request.Url.PathAndQuery}</p>
<p>请求方法:{request.HttpMethod}</p>
<div class="aritcle_card">
<a class="aritcle_card_img" href="/ai/2337">
<img src="https://img.php.cn/upload/ai_manual/001/246/273/175946161464083.png" alt="必应图像创建器">
</a>
<div class="aritcle_card_info">
<a href="/ai/2337">必应图像创建器</a>
<p>微软必应出品的AI绘图工具</p>
<div class="">
<img src="/static/images/card_xiazai.png" alt="必应图像创建器">
<span>593</span>
</div>
</div>
<a href="/ai/2337" class="aritcle_card_btn">
<span>查看详情</span>
<img src="/static/images/cardxiayige-3.png" alt="必应图像创建器">
</a>
</div>
<p>客户端IP:{request.RemoteEndPoint}</p>
</body>
</html>";
byte[] buffer = System.Text.Encoding.UTF8.GetBytes(responseString);
response.ContentLength64 = buffer.Length;
response.ContentType = "text/html; charset=utf-8";
response.OutputStream.Write(buffer, 0, buffer.Length);
response.Close();}
首次使用指定端口时,系统可能拒绝访问。你需要以管理员身份运行命令提示符,执行以下命令:
netsh http add urlacl url=https://www.php.cn/link/cbb686245ece57c9827c4bc0d0654a8e/ user=everyone
完成后,你的程序才能绑定到该地址。如果想监听所有IP,可用:
netsh http add urlacl url=http://*:8080/ user=everyone
不再使用时可删除注册:
netsh http delete urlacl url=https://www.php.cn/link/cbb686245ece57c9827c4bc0d0654a8e/
将所有部分组合起来:
static void Main(string[] args)
{
var server = new SimpleWebServer();
server.Start("https://www.php.cn/link/cbb686245ece57c9827c4bc0d0654a8e/");
<pre class="brush:php;toolbar:false;"><pre class="brush:php;toolbar:false;">Console.WriteLine("按任意键停止服务器...");
Console.ReadKey();
if (server._listener?.IsListening == true)
server._listener.Stop();}
运行后打开浏览器访问 https://www.php.cn/link/cbb686245ece57c9827c4bc0d0654a8e,即可看到返回的HTML页面。
基本上就这些。你可以在 HandleRequest 中加入路由判断、静态文件返回、POST数据解析等功能,逐步扩展成更实用的小型服务端应用。
以上就是C#如何实现一个简单的Web服务器 C# HttpListener类的使用的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号