VBMAN.HttpClient - HTTP Client Object
Overview
VBMAN.HttpClient provides HTTP request functionality, supporting GET, POST and other request methods, as well as request header settings, Cookie management, etc.
Core Features
- Multiple Method Support: GET, POST, PUT, DELETE and other HTTP methods
- SSL/HTTPS: Supports secure HTTPS requests
- Auto Redirect: Automatically handles 3xx redirects
- Cookie Management: Manage Cookies through RequestHeaders
- Timeout Settings: Configurable connection and read timeouts
- Chain Calling: Fluent API design
Properties
| Property | Type | Description |
|---|---|---|
RequestHeaders | Scripting.Dictionary | Request headers dictionary |
RequestContentType | String | Request Content-Type |
RequestChartSet | String | Request charset |
RequestTimeOut | Long | Timeout (milliseconds) |
ResponseHeaders | Scripting.Dictionary | Response headers dictionary |
Cookies | Scripting.Dictionary | Cookie dictionary |
ResponseRaw | Variant | Raw response content |
LastError | String | Last error message |
RequestDataJson | cJson | JSON request data object |
RequestDataForm | Scripting.Dictionary | Form data dictionary |
RequestDataQuery | Scripting.Dictionary | URL parameter dictionary |
Methods
SendGet
Send GET request
vb
Public Function SendGet(ByVal url As String, Optional Body As String) As cHttpClientExample:
vb
VBMAN.HttpClient.SendGet("https://api.example.com/users")SendPost
Send POST request
vb
Public Function SendPost(ByVal url As String, Optional Body As String) As cHttpClientExample:
vb
VBMAN.HttpClient.SendPost("https://api.example.com/users", "name=test&age=20")SendPut
Send PUT request
vb
Public Function SendPut(ByVal url As String, Optional Body As String) As cHttpClientSendDelete
Send DELETE request
vb
Public Function SendDelete(ByVal url As String, Optional Body As String) As cHttpClientSendOptions
Send OPTIONS request
vb
Public Function SendOptions(ByVal url As String, Optional Body As String) As cHttpClientSend / Fetch
General request methods (underlying methods)
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
Set request Content-Type
vb
Public Function SetRequestContentType(ReqType As EnumRequestContentType, Optional ContentType As String) As StringRequest Type Enum:
ReqJson- application/jsonReqFormUrlEncoded- application/x-www-form-urlencodedReqFormMultipart- multipart/form-dataReqTextPlain- text/plainReqTextHtml- text/html
SetCookies
Set Cookie
vb
Public Function SetCookies(ByVal Value As String) As cHttpClientExample:
vb
VBMAN.HttpClient.SetCookies("sessionid=abc123; user=admin")Async
Set async mode
vb
Public Function Async(Bool As Boolean) As cHttpClientShowPage
Open webpage with default browser
vb
Public Sub ShowPage(url As String)Comprehensive Examples
Example 1: GET Request
vb
Private Sub GetUserList()
With VBMAN.HttpClient
.SendGet("https://jsonplaceholder.typicode.com/users")
If .LastError = "" Then
' Parse JSON response
Dim json As cJson
Set json = VBMAN.Json.Decode(.ResponseRaw)
Debug.Print "Fetch successful"
Else
MsgBox "Request failed: " & .LastError
End If
End With
End SubExample 2: POST Request (JSON)
vb
Private Sub CreateUser(name As String, email As String)
With VBMAN.HttpClient
' Set Content-Type
.SetRequestContentType ReqJson
' Build JSON request body
With .RequestDataJson
.Item("name") = name
.Item("email") = email
End With
.SendPost "https://api.example.com/users", .RequestDataJson.Encode
If .LastError = "" Then
Debug.Print "Create successful"
End If
End With
End SubExample 3: Form Submission
vb
Private Sub SubmitForm(username As String, password As String)
With VBMAN.HttpClient
' Set form data
.RequestDataForm("username") = username
.RequestDataForm("password") = password
' Convert to form format and send
.SendPost "https://api.example.com/login", VBMAN.ToolsHttp.MakeContent(.RequestDataForm)
End With
End SubExample 4: Request with Authentication
vb
Private Sub ApiRequestWithAuth()
With VBMAN.HttpClient
' Set request headers
.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 SubExample 5: URL Parameters
vb
Private Sub SearchUsers(keyword As String)
With VBMAN.HttpClient
' Add URL parameters
.RequestDataQuery("q") = keyword
.RequestDataQuery("page") = 1
.RequestDataQuery("limit") = 20
.SendGet "https://api.example.com/search"
End With
End SubBest Practices
- Timeout Setting: Set reasonable timeout based on network conditions
- Error Handling: Always check LastError
- HTTPS First: Always use HTTPS for sensitive data
- Header Management: Use RequestHeaders dictionary to set custom headers
- Encoding Handling: Pay attention to character encoding of requests and responses