Skip to content

VBMAN.HttpClient - HTTP 客户端对象

概述

VBMAN.HttpClient 提供了 HTTP 请求功能,支持 GET、POST 等多种请求方法,以及请求头设置、Cookie 管理等特性。

核心特性

  • 多方法支持: GET、POST、PUT、DELETE 等 HTTP 方法
  • SSL/HTTPS: 支持安全的 HTTPS 请求
  • 自动重定向: 自动处理 3xx 重定向
  • Cookie 管理: 通过 RequestHeaders 管理 Cookie
  • 超时设置: 可配置连接和读取超时
  • 链式调用: 流畅的 API 设计

属性

属性类型说明
RequestHeadersScripting.Dictionary请求头字典
RequestContentTypeString请求 Content-Type
RequestChartSetString请求字符集
RequestTimeOutLong超时时间(毫秒)
ResponseHeadersScripting.Dictionary响应头字典
CookiesScripting.DictionaryCookie 字典
ResponseRawVariant原始响应内容
LastErrorString最后错误信息
RequestDataJsoncJsonJSON 请求数据对象
RequestDataFormScripting.Dictionary表单数据字典
RequestDataQueryScripting.DictionaryURL 参数字典

方法

SendGet

发送 GET 请求

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

示例:

vb
VBMAN.HttpClient.SendGet("https://api.example.com/users")

SendPost

发送 POST 请求

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

示例:

vb
VBMAN.HttpClient.SendPost("https://api.example.com/users", "name=test&age=20")

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 / Fetch

通用请求方法(底层方法)

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

SetRequestContentType

设置请求 Content-Type

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

请求类型枚举:

  • ReqJson - application/json
  • ReqFormUrlEncoded - application/x-www-form-urlencoded
  • ReqFormMultipart - multipart/form-data
  • ReqTextPlain - text/plain
  • ReqTextHtml - text/html

SetCookies

设置 Cookie

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

示例:

vb
VBMAN.HttpClient.SetCookies("sessionid=abc123; user=admin")

Async

设置异步模式

vb
Public Function Async(Bool As Boolean) As cHttpClient

ShowPage

用默认浏览器打开网页

vb
Public Sub ShowPage(url As String)

综合示例

示例1: GET 请求

vb
Private Sub GetUserList()
    With VBMAN.HttpClient
        .SendGet("https://jsonplaceholder.typicode.com/users")
        
        If .LastError = "" Then
            ' 解析 JSON 响应
            Dim json As cJson
            Set json = VBMAN.Json.Decode(.ResponseRaw)
            Debug.Print "获取成功"
        Else
            MsgBox "请求失败: " & .LastError
        End If
    End With
End Sub

示例2: POST 请求(JSON)

vb
Private Sub CreateUser(name As String, email As String)
    With VBMAN.HttpClient
        ' 设置 Content-Type
        .SetRequestContentType ReqJson
        
        ' 构建 JSON 请求体
        With .RequestDataJson
            .Item("name") = name
            .Item("email") = email
        End With
        
        .SendPost "https://api.example.com/users", .RequestDataJson.Encode
        
        If .LastError = "" Then
            Debug.Print "创建成功"
        End If
    End With
End Sub

示例3: 表单提交

vb
Private Sub SubmitForm(username As String, password As String)
    With VBMAN.HttpClient
        ' 设置表单数据
        .RequestDataForm("username") = username
        .RequestDataForm("password") = password
        
        ' 转换为表单格式并发送
        .SendPost "https://api.example.com/login", VBMAN.ToolsHttp.MakeContent(.RequestDataForm)
    End With
End Sub

示例4: 带认证的请求

vb
Private Sub ApiRequestWithAuth()
    With VBMAN.HttpClient
        ' 设置请求头
        .RequestHeaders("Authorization") = "Bearer " & GetAccessToken()
        .RequestHeaders("X-Request-ID") = VBMAN.ToolsStr.GetGUID()
        
        .SendGet "https://api.example.com/protected"
        
        If .LastError = "" Then
            ProcessResponse .ResponseRaw
        End If
    End With
End Sub

示例5: URL 参数

vb
Private Sub SearchUsers(keyword As String)
    With VBMAN.HttpClient
        ' 添加 URL 参数
        .RequestDataQuery("q") = keyword
        .RequestDataQuery("page") = 1
        .RequestDataQuery("limit") = 20
        
        .SendGet "https://api.example.com/search"
    End With
End Sub

最佳实践

  1. 超时设置: 根据网络情况设置合理的超时时间
  2. 错误处理: 始终检查 LastError
  3. HTTPS 优先: 敏感数据始终使用 HTTPS
  4. 请求头管理: 使用 RequestHeaders 字典设置自定义头
  5. 编码处理: 注意请求和响应的字符编码

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