Skip to content

cHttpServer 组件概述

简介

cHttpServer 是一个基于 VB6 的 HTTP 服务器组件,提供完整的 Web 服务器功能,支持路由、Session、Cookie、静态文件服务、SSE (Server-Sent Events)、跨域处理等特性。

特性

特性说明
HTTP 服务支持 GET/POST/PUT/DELETE/OPTIONS 方法
路由系统支持手动路由注册和自动路由
Session 管理支持内存、文件系统、数据库三种存储方式
Cookie 处理完整的 Cookie 解析和设置
静态文件自动提供静态文件服务
SSE 支持Server-Sent Events 实时推送
跨域处理内置 CORS 跨域支持
MVC 架构支持控制器-视图-模式开发
粘包处理自动处理 TCP 粘包问题

架构概览

┌─────────────────────────────────────────────────────────┐
│                     cHttpServer                         │
│  ┌─────────────┐  ┌─────────────┐  ┌─────────────┐     │
│  │   Router    │  │   Session   │  │    SSE      │     │
│  │  (路由系统)  │  │  (会话管理)  │  │(实时推送)   │     │
│  └─────────────┘  └─────────────┘  └─────────────┘     │
│  ┌─────────────┐  ┌─────────────┐  ┌─────────────┐     │
│  │   Request   │  │  Response   │  │   Context   │     │
│  │  (请求对象)  │  │  (响应对象)  │  │  (上下文)    │     │
│  └─────────────┘  └─────────────┘  └─────────────┘     │
└─────────────────────────────────────────────────────────┘

快速开始

最小 HTTP 服务器

vb
Private WithEvents Server As cHttpServer

Private Sub Form_Load()
    Set Server = New cHttpServer
    
    ' 注册路由
    Call Server.Router.Reg("Home", New cHomeController)
    Call Server.Router.Add("/", "Home@Index")
    Call Server.Router.Add("/user", "Home@User", OnlyGet)
    Call Server.Router.Add("/api/data", "Home@Api", OnlyPost)
    
    ' 启动服务器
    If Server.Start(8080, "C:\WebRoot") Then
        Debug.Print "服务器启动成功: http://localhost:8080"
    Else
        Debug.Print "启动失败: " & Server.LastError
    End If
End Sub

Private Sub Form_Unload(Cancel As Integer)
    Server.StopMe
End Sub

' 控制器示例
Public Sub Index(ctx As cHttpServerContext)
    ctx.Response.Html "<h1>Hello World!</h1>"
End Sub

Public Sub Api(ctx As cHttpServerContext)
    Dim data As New Scripting.Dictionary
    data("message") = "Success"
    data("time") = Now
    ctx.Response.Json data, 0, "OK"
End Sub

Session 配置

vb
Private Sub Form_Load()
    Set Server = New cHttpServer
    
    ' 配置 Session 存储方式
    Server.SessionStorageType = SessionStorageFileSystem  ' 文件存储
    Server.SessionStoragePath = "C:\Sessions"             ' 存储路径
    Server.SessionCookieName = "MY_SESSIONID"             ' Cookie 名称
    
    ' 或使用数据库存储
    ' Server.SessionStorageType = SessionStorageDatabase
    ' Server.SessionStoragePath = "sessions_table"
    
    Call Server.Start(8080, "C:\WebRoot")
End Sub

跨域配置

vb
Private Sub Form_Load()
    Set Server = New cHttpServer
    
    ' 启用跨域
    Server.CrossDomain.Enable = True
    Server.CrossDomain.AllowOrigin = "*"
    Server.CrossDomain.AllowMethods = "GET, POST, PUT, DELETE"
    Server.CrossDomain.AllowHeaders = "Content-Type, Authorization"
    Server.CrossDomain.AllowCredentials = True
    
    Call Server.Start(8080)
End Sub

文件结构

文件说明
cHttpServer.cls主服务器类
cHttpServerRouter.cls路由器
cHttpServerRequest.cls请求对象
cHttpServerResponse.cls响应对象
cHttpServerSession.clsSession 管理
cHttpServerCookies.clsCookie 管理
cHttpServerContext.cls上下文对象
cHttpServerClientInfo.cls客户端信息
cHttpServerRouteBefore.cls前置路由
cHttpServerRouterAfter.cls后置路由
cHttpCrossDomain.cls跨域处理

引用组件

  • cTlsReMaster.cls - TLS/网络通信
  • cJson.cls - JSON 处理
  • cScriptEngine.cls - 脚本引擎
  • cDataBase.cls - 数据库(可选)
  • cSSE.cls - SSE 支持

最后更新: 2026-05-17

VB6及其LOGO版权为微软公司所有