cHttpServerResponse 响应对象
简介
cHttpServerResponse 用于构建和发送 HTTP 响应,支持文本、HTML、JSON、文件等多种响应类型。
响应方法
Text
发送纯文本响应。
vb
Public Sub Text(Data As String)参数:
Data- 响应文本内容
示例:
vb
ctx.Response.Text "Hello, World!"Html
发送 HTML 响应。
vb
Public Sub Html(Data As String)参数:
Data- HTML 内容
示例:
vb
ctx.Response.Html "<h1>Hello</h1><p>Welcome to my site</p>"Json
发送 JSON 响应。
vb
Public Sub Json(Data As Variant, Optional Code As Long = -1, Optional msg As String, Optional Count As Long, Optional Whitespace As Variant)参数:
Data- 要序列化为 JSON 的数据(Dictionary、Array 等)Code- 状态码(默认 -1 表示不包装)msg- 消息文本Count- 数据总数(用于分页)Whitespace- JSON 格式化选项
示例:
vb
' 简单 JSON
Dim data As New Scripting.Dictionary
data("name") = "张三"
data("age") = 25
ctx.Response.Json data
' 包装格式(API 标准响应)
' 输出: {"code": 0, "msg": "Success", "data": {...}, "count": 1}
ctx.Response.Json data, 0, "Success", 1
' 格式化输出
ctx.Response.Json data, , , , TrueFile
发送文件响应。
vb
Public Sub File(Path As String)参数:
Path- 文件路径(相对于 WebRoot)
示例:
vb
' 请求 /image/logo.png
ctx.Response.File "/image/logo.png"状态码方法
State
发送自定义状态码响应。
vb
Public Sub State(Code As Long, Text As String, Optional Say As String = "未知状态")State400
400 Bad Request。
vb
Public Sub State400(Optional Say As String)示例:
vb
If Not IsValidParams Then
ctx.Response.State400 "参数错误"
End IfState401
401 Unauthorized。
vb
Public Sub State401(Optional Say As String)示例:
vb
If Not IsAuthenticated Then
ctx.Response.State401 "请先登录"
End IfState403
403 Forbidden。
vb
Public Sub State403(Optional Say As String)示例:
vb
If Not HasPermission Then
ctx.Response.State403 "没有权限"
End IfState404
404 Not Found。
vb
Public Sub State404(Optional Say As String)示例:
vb
If Not UserExists(userId) Then
ctx.Response.State404 "用户不存在"
End IfState500
500 Internal Server Error。
vb
Public Sub State500(Optional Say As String)示例:
vb
On Error GoTo ErrorHandler
' ... 业务逻辑
Exit Sub
ErrorHandler:
ctx.Response.State500 Err.DescriptionState502
502 Gateway Error。
vb
Public Sub State502(Optional Say As String)配置属性
Header
自定义响应头部字典。
vb
Public Header As New Scripting.Dictionary示例:
vb
ctx.Response.Header("X-Custom-Header") = "CustomValue"
ctx.Response.Header("Cache-Control") = "no-cache"CharSet
字符编码(默认 utf-8)。
vb
Public CharSet As String示例:
vb
ctx.Response.CharSet = "gb2312"ContentType
内容类型。
vb
Public ContentType As String示例:
vb
ctx.Response.ContentType = "application/xml"Status
HTTP 状态码字符串。
vb
Public Status As StringJsonPackFieldNameCode
JSON 包装字段名 - 状态码。
vb
Public JsonPackFieldNameCode As String默认值: "code"
JsonPackFieldNameMsg
JSON 包装字段名 - 消息。
vb
Public JsonPackFieldNameMsg As String默认值: "msg"
JsonPackFieldNameData
JSON 包装字段名 - 数据。
vb
Public JsonPackFieldNameData As String默认值: "data"
JsonPackFieldNameCount
JSON 包装字段名 - 总数。
vb
Public JsonPackFieldNameCount As String默认值: "count"
示例:
vb
' 自定义 JSON 响应字段名
ctx.Response.JsonPackFieldNameCode = "status"
ctx.Response.JsonPackFieldNameMsg = "message"
ctx.Response.JsonPackFieldNameData = "result"
ctx.Response.JsonPackFieldNameCount = "total"
' 输出: {"status": 0, "message": "OK", "result": {...}, "total": 10}
ctx.Response.Json data, 0, "OK", 10兼容 ASP 的属性
| 属性 | 说明 |
|---|---|
Buffer | 缓冲输出 |
CacheControl | 缓存控制 |
CodePage | 代码页 |
Expires | 过期时间 |
ExpiresAbsolute | 绝对过期时间 |
LCID | 区域标识 |
最后更新: 2026-05-17