VBMAN.HttpClient - HTTP 客户端对象
概述
VBMAN.HttpClient 提供了 HTTP 请求功能,支持 GET、POST 等多种请求方法,以及请求头设置、Cookie 管理等特性。
核心特性
- 多方法支持: GET、POST、PUT、DELETE 等 HTTP 方法
- SSL/HTTPS: 支持安全的 HTTPS 请求
- 自动重定向: 自动处理 3xx 重定向
- Cookie 管理: 通过 RequestHeaders 管理 Cookie
- 超时设置: 可配置连接和读取超时
- 链式调用: 流畅的 API 设计
属性
| 属性 | 类型 | 说明 |
|---|---|---|
RequestHeaders | Scripting.Dictionary | 请求头字典 |
RequestContentType | String | 请求 Content-Type |
RequestChartSet | String | 请求字符集 |
RequestTimeOut | Long | 超时时间(毫秒) |
ResponseHeaders | Scripting.Dictionary | 响应头字典 |
Cookies | Scripting.Dictionary | Cookie 字典 |
ResponseRaw | Variant | 原始响应内容 |
LastError | String | 最后错误信息 |
RequestDataJson | cJson | JSON 请求数据对象 |
RequestDataForm | Scripting.Dictionary | 表单数据字典 |
RequestDataQuery | Scripting.Dictionary | URL 参数字典 |
方法
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 cHttpClientSendDelete
发送 DELETE 请求
vb
Public Function SendDelete(ByVal url As String, Optional Body As String) As cHttpClientSendOptions
发送 OPTIONS 请求
vb
Public Function SendOptions(ByVal url As String, Optional Body As String) As cHttpClientSend / 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 cHttpClientSetRequestContentType
设置请求 Content-Type
vb
Public Function SetRequestContentType(ReqType As EnumRequestContentType, Optional ContentType As String) As String请求类型枚举:
ReqJson- application/jsonReqFormUrlEncoded- application/x-www-form-urlencodedReqFormMultipart- multipart/form-dataReqTextPlain- text/plainReqTextHtml- 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 cHttpClientShowPage
用默认浏览器打开网页
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最佳实践
- 超时设置: 根据网络情况设置合理的超时时间
- 错误处理: 始终检查 LastError
- HTTPS 优先: 敏感数据始终使用 HTTPS
- 请求头管理: 使用 RequestHeaders 字典设置自定义头
- 编码处理: 注意请求和响应的字符编码