Skip to content

cHttpClient 方法参考 ​

🌐 HTTP 请求方法 ​

SendGet ​

发送 GET 请求。

vb
Public Function SendGet(ByVal url As String, Optional Body As String) As cHttpClient

参数:

  • url - 请求地址
  • Body - 可选,请求体内容

返回: 返回自身实例,支持链式调用

示例:

vb
Dim http As New cHttpClient

' 简单 GET
http.SendGet("https://api.example.com/users")

' 带查询参数的 GET
http.RequestDataQuery("page") = "1"
http.RequestDataQuery("limit") = "10"
http.SendGet("https://api.example.com/users")

SendPost ​

发送 POST 请求。

vb
Public Function SendPost(ByVal url As String, Optional Body As String) As cHttpClient

示例:

vb
' POST JSON 数据
Dim json As New cJson
json.AddItem "name", "张三"
Set http.RequestDataJson = json
http.SendPost("https://api.example.com/users")

' POST Form 数据
http.RequestDataForm("username") = "admin"
http.RequestDataForm("password") = "123456"
http.SendPost("https://api.example.com/login")

SendPut ​

发送 PUT 请求。

vb
Public Function SendPut(ByVal url As String, Optional Body As String) As cHttpClient

SendDelete ​

发送 DELETE 请求。

vb
Public Function SendDelete(ByVal url As String, Optional Body As String) As cHttpClient

SendOptions ​

发送 OPTIONS 请求。

vb
Public Function SendOptions(ByVal url As String, Optional Body As String) As cHttpClient

Send ​

通用发送方法,可指定 HTTP 方法。

vb
Public Function Send(Method As EnumRequestMethod, ByVal url As String, Optional Body As String) As cHttpClient

参数:

  • Method - 请求方法枚举值:
    • ReqGet - GET 请求
    • ReqPost - POST 请求
    • ReqPut - PUT 请求
    • ReqDelete - DELETE 请求
    • ReqOptions - OPTIONS 请求

Fetch ​

底层请求方法,Send 系列方法均调用此方法。

vb
Public Function Fetch(Method As EnumRequestMethod, ByVal url As String, Optional Body As String) As cHttpClient

🔗 链式调用方法 ​

Async ​

设置请求模式为同步或异步。

vb
Public Function Async(Bool As Boolean) As cHttpClient

示例:

vb
' 异步请求
http.Async(True).SendGet("https://api.example.com/data")

' 同步请求(默认)
http.Async(False).SendGet("https://api.example.com/data")

Proxy ​

设置代理服务器,空字符串表示取消代理配置(直连)。

vb
Public Function Proxy(ByVal ProxyServer As String, Optional ByVal BypassList As String) As cHttpClient

参数:

  • ProxyServer - 代理服务器地址,空字符串表示取消代理直连。格式:
    • "proxy.example.com:8080" - 所有协议共用同一代理
    • "http=proxy1:8080;https=proxy2:8443" - 分别为 HTTP/HTTPS 指定代理
  • BypassList - 可选,代理绕过地址列表,多个用分号分隔,如 "localhost;127.0.0.1;*.internal.com"

说明:

  • 应在 SendGet/SendPost 等请求方法之前调用
  • 设置后对所有后续请求生效,直到再次调用 Proxy 修改
  • 空字符串时使用 SetProxy 2(直连,不使用任何代理)

示例:

vb
Dim http As New cHttpClient

' 使用代理
http.Proxy("proxy.example.com:8080").SendGet("https://api.example.com/data")

' 分别为 HTTP/HTTPS 指定代理
http.Proxy("http=proxy1:8080;https=proxy2:8443").SendGet("...")

' 带绕过列表
http.Proxy("proxy.example.com:8080", "localhost;127.0.0.1;*.internal.com").SendGet("...")

' 取消代理,直连
http.Proxy("").SendGet("https://api.example.com/data")

SetCookies ​

设置请求 Cookies,支持 "name=value; name2=value2" 格式自动解析到 Cookies 字典。

vb
Public Function SetCookies(ByVal Value As String) As cHttpClient

示例:

vb
' 设置 Cookie(自动解析到字典)
http.SetCookies("session_id=abc123; user=admin")

' Cookie 会在后续请求中自动携带
http.SendGet("https://api.example.com/profile")

' 也可以直接操作 Cookies 字典
http.Cookies("token") = "xyz789"

SetRequestContentType ​

设置请求内容类型。

vb
Public Function SetRequestContentType(ReqType As EnumRequestContentType, Optional ContentType As String) As String

参数:

  • ReqType - 内容类型枚举:
    • ReqContentTypeNone - 无
    • ReqContentTypeJson - application/json
    • ReqContentTypeFormUrlencoded - application/x-www-form-urlencoded
    • ReqContentTypeFormMultipart - multipart/form-data
    • ReqContentTypeTextPlain - text/plain
    • ReqContentTypeTextHtml - text/html

示例:

vb
http.SetRequestContentType(ReqContentTypeJson)

MapRequestContentType ​

内容类型映射方法,与 SetRequestContentType 相同。


📤 响应获取方法 ​

ReturnText ​

获取响应文本内容。

vb
Public Function ReturnText(Optional IsUtf8 As Boolean = True, Optional IsConvert As Boolean) As String

参数:

  • IsUtf8 - 是否按 UTF-8 解码(默认 True)
  • IsConvert - 是否使用 StrConv 转换(用于解决乱码)

示例:

vb
Dim text As String
text = http.SendGet("https://api.example.com").ReturnText()

' 处理可能的乱码
text = http.ReturnText(False, True)

ReturnJson ​

获取响应并解析为 JSON 对象。

vb
Public Function ReturnJson(Optional IsUtf8 As Boolean = True, Optional IsConvert As Boolean) As cJson

示例:

vb
Dim json As cJson
Set json = http.SendGet("https://api.example.com/users").ReturnJson()

If json.HasKey("data") Then
    Debug.Print json.GetItem("data")
End If

ReturnBody ​

获取原始响应字节数组。

vb
Public Function ReturnBody() As Byte()

示例:

vb
Dim body() As Byte
body = http.SendGet("https://api.example.com/file").ReturnBody()

ReturnStream ​

获取响应流对象。

vb
Public Function ReturnStream() As Variant

🛠️ 工具方法 ​

ShowPage ​

使用默认浏览器打开 URL。

vb
Public Sub ShowPage(url As String)

示例:

vb
http.ShowPage("https://www.example.com")

📊 调试信息 ​

DebugInfo ​

获取调试信息 JSON 对象(需先设置 DebugStart = True)。

vb
Public DebugInfo As cJson

示例:

vb
http.DebugStart = True
http.SendGet("https://api.example.com")

' 查看调试信息
If Not http.DebugInfo.RootIsEmpty Then
    ' Root 是默认成员,可直接用 Json("key") 访问
    Debug.Print http.DebugInfo("Request")("Url")
    Debug.Print http.DebugInfo("Response")("Status")
    Debug.Print http.DebugInfo("Error")("Description")
End If

🎯 事件 ​

OnResponseFinished ​

异步请求响应完成时触发。

vb
Public Event OnResponseFinished()

触发条件:HTTP 2xx 响应完成时触发。3xx 重定向不触发此事件(由 OnRedirect 处理),4xx/5xx 不触发此事件(由 OnError 处理)。

示例:

vb
Private WithEvents HttpClient As cHttpClient

Private Sub Form_Load()
    Set HttpClient = New cHttpClient
End Sub

Private Sub HttpClient_OnResponseFinished()
    Debug.Print "请求完成: " & HttpClient.ReturnText()
    Debug.Print "状态码: " & HttpClient.StatusCode
End Sub

OnError ​

异步请求发生错误时触发,包括网络错误和 HTTP 4xx/5xx 响应。

vb
Public Event OnError(ByVal ErrorNumber As Long, ByVal ErrorDescription As String)

说明:

  • 网络错误(连接失败等)会触发此事件
  • HTTP 4xx/5xx 响应也会触发此事件(不再触发 OnResponseFinished)
  • ErrorNumber 为 HTTP 状态码或 WinHttp 错误码
  • 超时错误码 12002 会触发 OnTimeout 而非此事件

示例:

vb
Private Sub HttpClient_OnError(ByVal ErrorNumber As Long, ByVal ErrorDescription As String)
    Debug.Print "请求失败 [" & ErrorNumber & "]: " & ErrorDescription
End Sub

OnResponseStart ​

异步请求收到响应头时触发。

vb
Public Event OnResponseStart(ByVal Status As Long, ByVal ContentType As String)

参数:

  • Status - HTTP 状态码(如 200、404)
  • ContentType - 响应 Content-Type 头的值

说明: 内部会解析 Content-Length 头用于进度追踪。3xx 状态码时不触发此事件,改为触发 OnRedirect。

示例:

vb
Private Sub HttpClient_OnResponseStart(ByVal Status As Long, ByVal ContentType As String)
    Debug.Print "响应开始: Status=" & Status & " Type=" & ContentType
End Sub

OnResponseDataAvailable ​

异步请求收到响应数据时触发,可用于下载进度追踪。

vb
Public Event OnResponseDataAvailable(Data() As Byte, ByVal BytesReceived As Long, ByVal TotalBytes As Long)

参数:

  • Data - 本次收到的数据分片
  • BytesReceived - 累计已接收字节数
  • TotalBytes - 总字节数(来自 Content-Length,未知时为 -1)

示例:

vb
Private Sub HttpClient_OnResponseDataAvailable(Data() As Byte, ByVal BytesReceived As Long, ByVal TotalBytes As Long)
    If TotalBytes > 0 Then
        Debug.Print "进度: " & BytesReceived & "/" & TotalBytes & " (" & Format(BytesReceived / TotalBytes * 100, "0.0") & "%)"
    Else
        Debug.Print "已接收: " & BytesReceived & " 字节"
    End If
End Sub

OnRedirect ​

异步请求收到 3xx 重定向时触发(需设置 FollowRedirects = False)。

vb
Public Event OnRedirect(ByVal Url As String)

参数:

  • Url - 重定向目标 URL(来自 Location 响应头)

说明:

  • 仅在 FollowRedirects = False 时触发
  • 3xx 响应不会触发 OnResponseFinished,改由 OnRedirect 处理
  • 可在事件中手动决定是否发起新请求

示例:

vb
Dim http As New cHttpClient
http.FollowRedirects = False
http.Async(True).SendGet "https://api.example.com/old-url"

Private Sub http_OnRedirect(ByVal Url As String)
    Debug.Print "重定向到: " & Url
    ' 手动发起新请求
    http.Async(True).SendGet Url
End Sub

OnTimeout ​

异步请求超时时触发。

vb
Public Event OnTimeout()

说明: 当 WinHttp 返回超时错误码(12002)时触发,替代 OnError 事件,便于实现重试逻辑。

示例:

vb
Private Sub HttpClient_OnTimeout()
    Debug.Print "请求超时,准备重试..."
    HttpClient.Async(True).SendGet "https://api.example.com/slow-api"
End Sub

最后更新: 2026-07-13

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