Skip to content

cHttpServerResponse Response Object ​

Overview ​

cHttpServerResponse is used to build and send HTTP responses, supporting text, HTML, JSON, files, and other response types.

Response Methods ​

Text ​

Send plain text response.

vb
Public Sub Text(Data As String)

Parameters:

  • Data - Response text content

Example:

vb
ctx.Response.Text "Hello, World!"

Html ​

Send HTML response.

vb
Public Sub Html(Data As String)

Parameters:

  • Data - HTML content

Example:

vb
ctx.Response.Html "<h1>Hello</h1><p>Welcome to my site</p>"

Json ​

Send JSON response.

vb
Public Sub Json(Data As Variant, Optional Code As Long = -1, Optional msg As String, Optional Count As Long, Optional Whitespace As Variant)

Parameters:

  • Data - Data to serialize to JSON (Dictionary, Array, etc.)
  • Code - Status code (default -1 means no wrapper)
  • msg - Message text
  • Count - Total data count (for pagination)
  • Whitespace - JSON formatting option

Example:

vb
' Simple JSON
Dim data As New Dictionary
data("name") = "John"
data("age") = 25
ctx.Response.Json data

' Wrapped format (API standard response)
' Output: {"code": 0, "msg": "Success", "data": {...}, "count": 1}
ctx.Response.Json data, 0, "Success", 1

' Formatted output
ctx.Response.Json data, , , , True

File ​

Send file response (with built-in ETag + 304 negotiated cache + lazy content cache).

vb
Public Sub File(Path As String)

Parameters:

  • Path - File path (relative to WebRoot)

Description:

  • Auto-calculates ETag (based on file modification time + size) and sets ETag, Last-Modified response headers
  • When browser sends If-None-Match that matches, returns 304 Not Modified (zero transfer)
  • When file size ≤ MaxCacheFileSize, auto-adds to memory cache (lazy cache), subsequent access outputs from memory skipping disk I/O
  • Files exceeding cache limit are still read from disk, but enjoy ETag/304 negotiated cache
  • When file is updated on disk, ETag auto-changes and old cache auto-invalidates
  • When path corresponds to a directory, auto-searches default documents (index.html etc); returns 403 if no default document found

Example:

vb
' Send static file (auto with cache headers)
ctx.Response.File "/css/style.css"

' Access directory (auto-searches index.html)
ctx.Response.File "/"

' Large file (exceeds cache limit, always read from disk, but with ETag/304)
ctx.Response.File "/videos/demo.mp4"

Cache Mechanism Details:

Request → Calculate ETag (FSO metadata, no file content read)
  ├─> If-None-Match matches → 304 (zero transfer)
  ├─> Cache hit + ETag unchanged → Memory output (skip disk)
  ├─> Cache hit + ETag changed → Re-read + update cache
  └─> No cache → Disk read + lazy cache (only add if ≤ limit)

See Static File Serving - Cache Control


Status Code Methods ​

State ​

Send custom status code response.

vb
Public Sub State(Code As Long, Text As String, Optional Say As String = "Unknown status")

State400 ​

400 Bad Request.

vb
Public Sub State400(Optional Say As String)

Example:

vb
If Not IsValidParams Then
    ctx.Response.State400 "Invalid parameters"
End If

State401 ​

401 Unauthorized.

vb
Public Sub State401(Optional Say As String)

Example:

vb
If Not IsAuthenticated Then
    ctx.Response.State401 "Please login first"
End If

State403 ​

403 Forbidden.

vb
Public Sub State403(Optional Say As String)

Example:

vb
If Not HasPermission Then
    ctx.Response.State403 "No permission"
End If

State404 ​

404 Not Found.

vb
Public Sub State404(Optional Say As String)

Example:

vb
If Not UserExists(userId) Then
    ctx.Response.State404 "User not found"
End If

State413 ​

413 Payload Too Large.

vb
Public Sub State413(Optional Say As String = "Request Entity Too Large")

Description: This status code is automatically returned when the request body exceeds the Server.MaxRequestSize limit. Can also be called manually.

Example:

vb
' Check upload file size
If FileSize > MaxAllowedSize Then
    ctx.Response.State413 "File size exceeds limit"
End If

State500 ​

500 Internal Server Error.

vb
Public Sub State500(Optional Say As String)

Example:

vb
On Error GoTo ErrorHandler
' ... Business logic
Exit Sub

ErrorHandler:
    ctx.Response.State500 Err.Description

State502 ​

502 Gateway Error.

vb
Public Sub State502(Optional Say As String)

Configuration Properties ​

Custom response header dictionary.

vb
Public Header As New Dictionary

Example:

vb
ctx.Response.Header("X-Custom-Header") = "CustomValue"
ctx.Response.Header("Cache-Control") = "no-cache"

CharSet ​

Character encoding (default utf-8).

vb
Public CharSet As String

Example:

vb
ctx.Response.CharSet = "gb2312"

ContentType ​

Content type.

vb
Public ContentType As String

Example:

vb
ctx.Response.ContentType = "application/xml"

Status ​

HTTP status string.

vb
Public Status As String

JsonPackFieldNameCode ​

JSON wrapper field name - status code.

vb
Public JsonPackFieldNameCode As String

Default: "code"


JsonPackFieldNameMsg ​

JSON wrapper field name - message.

vb
Public JsonPackFieldNameMsg As String

Default: "msg"


JsonPackFieldNameData ​

JSON wrapper field name - data.

vb
Public JsonPackFieldNameData As String

Default: "data"


JsonPackFieldNameCount ​

JSON wrapper field name - total count.

vb
Public JsonPackFieldNameCount As String

Default: "count"

Example:

vb
' Custom JSON response field names
ctx.Response.JsonPackFieldNameCode = "status"
ctx.Response.JsonPackFieldNameMsg = "message"
ctx.Response.JsonPackFieldNameData = "result"
ctx.Response.JsonPackFieldNameCount = "total"

' Output: {"status": 0, "message": "OK", "result": {...}, "total": 10}
ctx.Response.Json data, 0, "OK", 10

ASP-compatible Properties ​

PropertyDescription
BufferOutput buffering
CacheControlCache control
CodePageCode page
ExpiresExpiration time
ExpiresAbsoluteAbsolute expiration time
LCIDLocale identifier

Last Updated: 2026-06-22

VB6 and LOGO copyright of Microsoft Corporation