使用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,否则由于证书和域名不匹配导致无法继续。

相关推荐
缺点内向1 天前
如何在 C# 中将 Word 文档转换为 EMF(增强型图元文件)
开发语言·c#·word·.net
2501_915918411 天前
使用 HBuilder 上架 iOS 应用时常见的问题与应对方式
android·ios·小程序·https·uni-app·iphone·webview
MyBFuture1 天前
C# 哈希表与堆栈队列实战指南
开发语言·windows·c#·visual studio
写代码的橘子n1 天前
IPV6复习(基础入手版)
运维·服务器·网络
猫不吃咸鱼1 天前
Unity中攻击检测敌人的多种方法
游戏·unity·c#·游戏引擎
向日葵.1 天前
中间件交接文档
linux·运维·服务器
2501_916007471 天前
iOS 崩溃日志的分析方法,将崩溃日志与运行过程结合分析
android·ios·小程序·https·uni-app·iphone·webview
TG:@yunlaoda360 云老大1 天前
华为云国际站代理商TaurusDB的读写分离是如何实现的?
服务器·数据库·华为云
Blurpath1 天前
什么是 ISP 代理?双 ISP 代理又是什么?一文讲清原理与应用场景
服务器·网络·ip代理·静态代理·住宅代理
TG:@yunlaoda360 云老大1 天前
华为云国际站代理商如何配置GaussDB的读写分离权重?
服务器·华为云·gaussdb