Skip to content

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

PropertyTypeDescription
RequestHeadersScripting.DictionaryRequest headers dictionary
RequestContentTypeStringRequest Content-Type
RequestChartSetStringRequest charset
RequestTimeOutLongTimeout (milliseconds)
ResponseHeadersScripting.DictionaryResponse headers dictionary
CookiesScripting.DictionaryCookie dictionary
ResponseRawVariantRaw response content
LastErrorStringLast error message
RequestDataJsoncJsonJSON request data object
RequestDataFormScripting.DictionaryForm data dictionary
RequestDataQueryScripting.DictionaryURL parameter dictionary

Methods

SendGet

Send GET request

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

Example:

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 cHttpClient

Example:

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 cHttpClient

SendDelete

Send DELETE request

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

SendOptions

Send OPTIONS request

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

Send / 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 cHttpClient

SetRequestContentType

Set request Content-Type

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

Request Type Enum:

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

SetCookies

Set Cookie

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

Example:

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

Async

Set async mode

vb
Public Function Async(Bool As Boolean) As cHttpClient

ShowPage

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 Sub

Example 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 Sub

Example 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 Sub

Example 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 Sub

Example 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 Sub

Best Practices

  1. Timeout Setting: Set reasonable timeout based on network conditions
  2. Error Handling: Always check LastError
  3. HTTPS First: Always use HTTPS for sensitive data
  4. Header Management: Use RequestHeaders dictionary to set custom headers
  5. Encoding Handling: Pay attention to character encoding of requests and responses

VB6 and LOGO copyright of Microsoft Corporation