Skip to content

cHttpClient 属性参考

📝 请求数据属性

RequestDataJson

设置 JSON 格式的请求体数据。

vb
Public RequestDataJson As New cJson

说明: 当设置了此属性且 Content-Type 为 JSON 时,请求体会自动序列化为 JSON 字符串。

示例:

vb
Dim http As New cHttpClient
Dim json As New cJson

json.AddItem "username", "admin"
json.AddItem "password", "123456"

Set http.RequestDataJson = json
http.SendPost("https://api.example.com/login")

RequestDataForm

设置 Form 格式的请求体数据(表单数据)。

vb
Public RequestDataForm As New Scripting.Dictionary

说明: 适用于 application/x-www-form-urlencoded 类型的请求。

示例:

vb
Dim http As New cHttpClient

http.RequestDataForm("username") = "admin"
http.RequestDataForm("password") = "123456"
http.RequestDataForm("remember") = "true"

http.SendPost("https://api.example.com/login")

RequestDataQuery

设置 URL 查询参数。

vb
Public RequestDataQuery As New Scripting.Dictionary

说明: 发送请求时会自动追加到 URL 后面。

示例:

vb
Dim http As New cHttpClient

http.RequestDataQuery("page") = "1"
http.RequestDataQuery("limit") = "20"
http.RequestDataQuery("sort") = "desc"

' 最终 URL: https://api.example.com/users?page=1&limit=20&sort=desc
http.SendGet("https://api.example.com/users")

RequestDataBody

已废弃 - 请使用 RequestDataForm 替代。


📋 请求配置属性

RequestHeaders

设置自定义请求头。

vb
Public RequestHeaders As New Scripting.Dictionary

示例:

vb
Dim http As New cHttpClient

http.RequestHeaders("Authorization") = "Bearer token123"
http.RequestHeaders("X-Custom-Header") = "custom-value"
http.RequestHeaders("User-Agent") = "MyApp/1.0"

http.SendGet("https://api.example.com/protected")

RequestContentType

设置请求内容类型。

vb
Public RequestContentType As String

常用值:

  • application/json - JSON 数据
  • application/x-www-form-urlencoded - 表单数据(默认)
  • multipart/form-data - 文件上传
  • text/plain - 纯文本
  • text/html - HTML 内容

示例:

vb
Dim http As New cHttpClient
http.RequestContentType = "application/json"
' 或使用便捷方法
http.SetRequestContentType(ReqContentTypeJson)

RequestChartSet

设置请求编码。

vb
Public RequestChartSet As String

默认值: utf-8

说明: 影响请求体的编码方式。设置为 UTF-8 时会使用 UTF-8 编码。


RequestTimeOut

设置请求超时时间(秒)。

vb
Public RequestTimeOut As Long

默认值: 30 秒

说明: 仅用于同步请求模式。

示例:

vb
Dim http As New cHttpClient
http.RequestTimeOut = 60  ' 60秒超时
http.SendGet("https://api.example.com/slow-api")

📥 响应数据属性

ResponseRaw

缓存的原始响应内容。

vb
Public ResponseRaw As Variant

ResponseHeaders

响应头字典。

vb
Public ResponseHeaders As New Scripting.Dictionary

示例:

vb
Dim http As New cHttpClient
http.SendGet("https://api.example.com")

' 获取响应头
If http.ResponseHeaders.Exists("Content-Type") Then
    Debug.Print "内容类型: " & http.ResponseHeaders("Content-Type")
End If

' 遍历所有响应头
Dim key As Variant
For Each key In http.ResponseHeaders.Keys
    Debug.Print key & ": " & http.ResponseHeaders(key)
Next

Cookies

解析后的 Cookies 字典。

vb
Public Cookies As New Scripting.Dictionary

说明: 自动从响应头 Set-Cookie 解析。

示例:

vb
Dim http As New cHttpClient
http.SendGet("https://api.example.com/login")

' 获取 Cookie
If http.Cookies.Exists("session_id") Then
    Debug.Print "Session ID: " & http.Cookies("session_id")
End If

' 下次请求时携带 Cookie
http.SetCookies("session_id=" & http.Cookies("session_id"))
http.SendGet("https://api.example.com/profile")

⚙️ 调试属性

DebugStart

启用调试模式。

vb
Public DebugStart As Boolean

说明: 设置为 True 后,请求完成后会填充 DebugInfo 属性。

示例:

vb
Dim http As New cHttpClient
http.DebugStart = True

On Error Resume Next
http.SendGet("https://api.example.com")

' 查看调试信息
If http.DebugInfo.HasKey("Error") Then
    Debug.Print "错误: " & http.DebugInfo.GetItem("Error").GetItem("Description")
End If

LastError

最后发生的错误信息。

vb
Public LastError As String

示例:

vb
Dim http As New cHttpClient
Dim success As Boolean

On Error Resume Next
success = http.SendGet("https://invalid-url").ReturnText() <> ""

If Not success Then
    Debug.Print "请求失败: " & http.LastError
End If

最后更新: 2026-05-17

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