Skip to content

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, , , , True

File

发送文件响应。

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 If

State401

401 Unauthorized。

vb
Public Sub State401(Optional Say As String)

示例:

vb
If Not IsAuthenticated Then
    ctx.Response.State401 "请先登录"
End If

State403

403 Forbidden。

vb
Public Sub State403(Optional Say As String)

示例:

vb
If Not HasPermission Then
    ctx.Response.State403 "没有权限"
End If

State404

404 Not Found。

vb
Public Sub State404(Optional Say As String)

示例:

vb
If Not UserExists(userId) Then
    ctx.Response.State404 "用户不存在"
End If

State500

500 Internal Server Error。

vb
Public Sub State500(Optional Say As String)

示例:

vb
On Error GoTo ErrorHandler
' ... 业务逻辑
Exit Sub

ErrorHandler:
    ctx.Response.State500 Err.Description

State502

502 Gateway Error。

vb
Public Sub State502(Optional Say As String)

配置属性

自定义响应头部字典。

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 String

JsonPackFieldNameCode

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

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