使用C#写的HTTPS简易服务器

由于监控网络之用,需要写一个https服务器。

由于用到https,因此还需一个域名证书,可以免费申请,也可以用一个现有的。

接下来还需在Windows上安装证书,注册证书。

安装证书

找到证书,点击,一路安装即可。

注册证书

查询证书

bash 复制代码
certutil -store my

查询结果

bash 复制代码
================ 证书 1 ================
序列号: 52fa907d41a8439c403d2d9e97453da8
颁发者: CN=XinChaCha Trust OV CA, O=Beijing Xinchacha Credit Management Co., Ltd., C=CN
 NotBefore: 2024/12/20 8:00
 NotAfter: 2025/12/21 7:59
使用者: CN=*.cdut.edu.cn, O=成都理工大学, S=四川省, C=CN
非根证书
证书哈希(sha1): 48c0f63170597f65c566903ac4b3e176828a03d4
  密钥容器 = {318BC6DA-F81A-42CB-BAD8-9CF8ECC96560}
  唯一容器名称: 74750d459b9e33bdb358fe76f9cc6fe9_73d663f3-bf98-4bb1-a62b-794087b69f98
  提供程序 = Microsoft Enhanced Cryptographic Provider v1.0
私钥不能导出
通过了加密测试
CertUtil: -store 命令成功完成。

记录下"证书哈希(sha1)"值:48c0f63170597f65c566903ac4b3e176828a03d4

注册证书

bash 复制代码
netsh http add sslcert ipport=0.0.0.0:8443 certhash=48c0f63170597f65c566903ac4b3e176828a03d4 appid={12345678-1234-1234-1234-123456789012}

替换 certhash。

appid,就是一个guid,只要不重复就好了。

编写测试代码

csharp 复制代码
 static void Main(string[] args)
 {
     // 证书路径和密码
     string certificatePath = "FullSSL.pfx";
     string certificatePassword = "12345";

     // 创建证书对象
     X509Certificate2 certificate = new X509Certificate2(certificatePath, certificatePassword);

     // 创建HttpListener
     HttpListener listener = new HttpListener();

     // 添加HTTPS前缀(注意:需要在系统上注册此URL)
     listener.Prefixes.Add("https://+:8443/");

     // 启动监听
     listener.Start();
     Console.WriteLine("Listening on https://+:8443/");

     // 处理请求
     while (true)
     {
         HttpListenerContext context = listener.GetContext();
         HttpListenerRequest request = context.Request;
         HttpListenerResponse response = context.Response;

         // 读取请求信息
         Console.WriteLine($"Request from {request.RemoteEndPoint}");
         //遍历headers
         foreach (string key in request.Headers.AllKeys)
         {
             Console.WriteLine($"{key}: {request.Headers[key]}");
         }

         Console.WriteLine($"Method: {request.HttpMethod}");
         Console.WriteLine($"URL: {request.Url}");

         // 创建响应
         byte[] buffer = Encoding.UTF8.GetBytes("Hello, this is an HTTPS server!");
         response.ContentLength64 = buffer.Length;
         response.OutputStream.Write(buffer, 0, buffer.Length);

         // 关闭响应
         response.Close();
     }
 }

测试

bash 复制代码
C:\Users\Zmrbak>curl -k https://127.0.0.1:8443/
Hello, this is an HTTPS server!

这里需要加参数 -k,否则由于证书和域名不匹配导致无法继续。

相关推荐
墨理学AI6 小时前
虚拟机导致的服务器交换内容占用过大(服务器卡顿)问题 快速解决
运维·服务器
程序猿老A7 小时前
轻量应用服务器怎么搭建个人网站?
服务器
盛世宏博智慧档案7 小时前
一站式智慧档案解决方案,如何实现防火防盗漏水多维度安全监测实践
服务器·网络·数据库
熊猫钓鱼>_>7 小时前
平行视界兼容模式与魔法变量配置避坑【鸿蒙心迹】
运维·服务器·开发语言·笔记·华为·harmonyos·openharmony
niuyueyang8 小时前
MCP `/mcp/status` Lifecycle 技术方案
java·服务器·数据库
曦夜日长8 小时前
主从 Reactor 高并发服务器实战 (三):服务器套接字Socket模块详解与实现
运维·服务器·网络
老约家的可汗9 小时前
Linux开发三件套:make、git、gdb 入门指南
linux·运维·服务器
从琳开始98910 小时前
从Socket到TCP回显服务器:Java网络编程完整指南
java·服务器·网络·tcp/ip·java-ee
云-缩影10 小时前
做微信小程序,自建服务器还是用云托管:技术选型对比
服务器·微信小程序·小程序
BuHuaX10 小时前
Lua 与 ToLua:运行机制及热更新原理
开发语言·unity·junit·c#·lua