cHttpServer 方法参考
🚀 服务器控制方法
Start
启动 HTTP 服务器。
vb
Public Function Start(Optional Port As Long = 80, Optional WebRoot As String = "", Optional IP As String = "0.0.0.0") As Boolean参数:
Port- 监听端口(默认 80)WebRoot- 静态文件根目录IP- 监听 IP 地址(默认 0.0.0.0 表示所有接口)
返回: 成功返回 True,失败返回 False
示例:
vb
' 基本启动
If Server.Start(8080) Then
Debug.Print "服务器启动成功"
End If
' 带静态文件目录
If Server.Start(8080, "C:\WebRoot") Then
Debug.Print "服务器启动成功"
End If
' 指定 IP
If Server.Start(8080, "C:\WebRoot", "127.0.0.1") Then
Debug.Print "本地服务器启动成功"
End IfStopMe
停止 HTTP 服务器。
vb
Public Function StopMe() As Boolean说明: 关闭所有连接,释放资源。
示例:
vb
Private Sub Form_Unload(Cancel As Integer)
Server.StopMe
Set Server = Nothing
End Sub🛣️ 路由相关方法(通过 Router 对象)
Reg
注册控制器。
vb
Public Function Reg(ControllerName As String, Controller As Object) As Boolean参数:
ControllerName- 控制器名称Controller- 控制器对象实例
示例:
vb
' 注册控制器
Call Server.Router.Reg("User", New cUserController)
Call Server.Router.Reg("Api", New cApiController)Add
添加路由规则。
vb
Public Function Add(RouteName As String, Handler As String, Optional MethodLimit As EnumRouteMethod = Any_) As Boolean参数:
RouteName- 路由路径(如 "/user")Handler- 处理程序(格式:"控制器名@方法名")MethodLimit- HTTP 方法限制:Any_- 任意方法(默认)OnlyGet- 仅 GETOnlyPost- 仅 POSTOnlyPut- 仅 PUTOnlyDelete- 仅 DELETE
示例:
vb
' 基本路由
Call Server.Router.Add("/", "Home@Index")
Call Server.Router.Add("/user", "User@List", OnlyGet)
Call Server.Router.Add("/user/create", "User@Create", OnlyPost)
Call Server.Router.Add("/user/update", "User@Update", OnlyPut)
Call Server.Router.Add("/user/delete", "User@Delete", OnlyDelete)📡 事件
OnAccept
新连接接入时触发。
vb
Public Event OnAccept(ClientInfo As cHttpServerClientInfo, Disconnect As Boolean)参数:
ClientInfo- 客户端信息对象Disconnect- 设置为 True 可拒绝连接
示例:
vb
Private Sub Server_OnAccept(ClientInfo As cHttpServerClientInfo, Disconnect As Boolean)
Debug.Print "新连接: " & ClientInfo.IP & ":" & ClientInfo.Port
' IP 黑名单检查
If ClientInfo.IP = "192.168.1.100" Then
Disconnect = True ' 拒绝连接
End If
End SubOnLogs
日志事件。
vb
Public Event OnLogs(ByVal Level As String, ByVal Content As String)示例:
vb
Private Sub Server_OnLogs(ByVal Level As String, ByVal Content As String)
Debug.Print "[" & Level & "] " & Content
End Sub🔧 控制器方法编写规范
控制器方法接收一个上下文参数,包含请求和响应对象:
vb
Public Sub ActionName(ctx As cHttpServerContext)
' ctx.Request - 请求对象
' ctx.Response - 响应对象
' ctx.Session - Session 对象
' ctx.Cookies - Cookies 对象
' ctx.Db - 数据库对象(如果配置了)
End Sub示例控制器:
vb
' cUserController.cls
Option Explicit
' GET /user
Public Sub List(ctx As cHttpServerContext)
Dim users As New Scripting.Dictionary
users("items") = Array("张三", "李四", "王五")
users("total") = 3
ctx.Response.Json users, 0, "Success"
End Sub
' POST /user/create
Public Sub Create(ctx As cHttpServerContext)
' 获取 POST 数据
Dim username As String
username = ctx.Request.Form("username")
' 获取 JSON 数据
' username = ctx.Request.Json.GetItem("username")
ctx.Response.Json Nothing, 0, "创建成功"
End Sub
' GET /user?id=1
Public Sub Detail(ctx As cHttpServerContext)
Dim id As String
id = ctx.Request.QueryString("id")
Dim user As New Scripting.Dictionary
user("id") = id
user("name") = "张三"
ctx.Response.Json user
End Sub📦 上下文对象属性
cHttpServerContext
| 属性 | 类型 | 说明 |
|---|---|---|
Request | cHttpServerRequest | 请求对象 |
Response | cHttpServerResponse | 响应对象 |
Session | cHttpServerSession | Session 对象 |
Cookies | cHttpServerCookies | Cookies 对象 |
Db | cDataBase | 数据库对象 |
Server | cHttpServerSvr | 服务器配置 |
ClientInfo | cHttpServerClientInfo | 客户端信息 |
最后更新: 2026-05-17