Skip to content

Tools - HTTP Utility Class

cToolsHttp - HTTP Encoding Tools

Overview

Provides URL encoding/decoding, HTTP-related string processing, query string construction, HTTP method mapping, and other functionality.


URL Encoding/Decoding (GB2312)

UrlEncode

Encodes a string using GB2312 URL encoding.

vb
Public Function UrlEncode(ByRef strURL As String) As String

Description:

  • Uses GB2312 encoding
  • Suitable for traditional encoding scenarios with Chinese characters

Example:

vb
Dim Encoded As String
Encoded = VBMAN.ToolsHttp.UrlEncode("你好世界")
Debug.Print Encoded  ' Output: %C4%E3%BA%C3%CA%C0%BD%E7

UrlDecode

Decodes a GB2312 URL-encoded string.

vb
Public Function UrlDecode(ByVal url As String) As String

Example:

vb
Dim Decoded As String
Decoded = VBMAN.ToolsHttp.UrlDecode("%C4%E3%BA%C3%CA%C0%BD%E7")
Debug.Print Decoded  ' Output: 你好世界

URL Encoding/Decoding (UTF-8)

UrlEncodeUtf8

Encodes a string using UTF-8 URL encoding (RFC3986 standard).

vb
Public Function UrlEncodeUtf8(ByVal szInput As Variant) As String

Description:

  • Follows RFC3986 standard
  • Safe characters: A-Z a-z 0-9 - _ . ~
  • Other characters are percent-encoded
  • Supports multi-byte UTF-8 characters

Example:

vb
Dim Encoded As String

' ASCII characters
Encoded = VBMAN.ToolsHttp.UrlEncodeUtf8("Hello World!")
Debug.Print Encoded  ' Output: Hello%20World%21

' Chinese characters
Encoded = VBMAN.ToolsHttp.UrlEncodeUtf8("你好世界")
Debug.Print Encoded  ' Output: %E4%BD%A0%E5%A5%BD%E4%B8%96%E7%95%8C

' Special characters
Encoded = VBMAN.ToolsHttp.UrlEncodeUtf8("a/b+c=d&e")
Debug.Print Encoded  ' Output: a%2Fb%2Bc%3Dd%26e

UrlDecodeUtf8

Decodes a UTF-8 URL-encoded string.

vb
Public Function UrlDecodeUtf8(ByVal url As String) As String

Example:

vb
Dim Decoded As String

Decoded = VBMAN.ToolsHttp.UrlDecodeUtf8("Hello%20World%21")
Debug.Print Decoded  ' Output: Hello World!

Decoded = VBMAN.ToolsHttp.UrlDecodeUtf8("%E4%BD%A0%E5%A5%BD%E4%B8%96%E7%95%8C")
Debug.Print Decoded  ' Output: 你好世界

Query String Operations

AddToQueryString

Adds query parameters to a URL.

vb
Public Function AddToQueryString(ByVal url As String, ByVal QS As String) As String

Description:

  • Automatically determines if URL already has query parameters
  • Automatically adds ? or &

Example:

vb
Dim Url As String

Url = VBMAN.ToolsHttp.AddToQueryString("http://api.example.com/user", "id=123")
Debug.Print Url  ' Output: http://api.example.com/user?id=123

Url = VBMAN.ToolsHttp.AddToQueryString("http://api.example.com/user?id=123", "name=John")
Debug.Print Url  ' Output: http://api.example.com/user?id=123&name=John

MakeContent

Builds a dictionary into query string format.

vb
Public Function MakeContent(Dic As Scripting.Dictionary, Optional IsUrlEncode As Boolean = True) As String

Parameters:

ParameterTypeDescription
DicScripting.DictionaryDictionary to convert
IsUrlEncodeBooleanWhether to URL-encode values (default True)

Example:

vb
Dim Params As New Scripting.Dictionary
Params.Add "name", "John"
Params.Add "age", "25"
Params.Add "city", "Beijing"

Dim QueryString As String
QueryString = VBMAN.ToolsHttp.MakeContent(Params)
Debug.Print QueryString  ' Output: name=John&age=25&city=Beijing

' Without encoding
QueryString = VBMAN.ToolsHttp.MakeContent(Params, False)
Debug.Print QueryString  ' Output: name=John&age=25&city=Beijing

ParseContent

Parses query string into dictionary.

vb
Public Function ParseContent(Content As String, Obj As Scripting.Dictionary, Optional IsUrlDecode As Boolean = True) As Boolean

Parameters:

ParameterTypeDescription
ContentStringQuery string
ObjScripting.DictionaryDictionary to store results
IsUrlDecodeBooleanWhether to URL-decode (default True)

Example:

vb
Dim Params As New Scripting.Dictionary
Dim Success As Boolean

Success = VBMAN.ToolsHttp.ParseContent("name=John&age=25", Params)

Debug.Print Params("name")  ' Output: John
Debug.Print Params("age")   ' Output: 25

ParseKeyValue

Parses key-value formatted string into dictionary (e.g., HTTP Headers).

vb
Public Function ParseKeyValue(Content As String, Obj As Scripting.Dictionary) As Boolean

Description:

  • Splits by lines
  • Each line splits by first : into key and value
  • Suitable for parsing HTTP Headers

Example:

vb
Dim Headers As New Scripting.Dictionary
Dim HeaderText As String

HeaderText = "Content-Type: application/json" & vbCrLf & _
             "Authorization: Bearer token123" & vbCrLf & _
             "X-Custom-Header: value"

VBMAN.ToolsHttp.ParseKeyValue HeaderText, Headers

Debug.Print Headers("Content-Type")     ' Output: application/json
Debug.Print Headers("Authorization")    ' Output: Bearer token123

Form URL-Encoded Conversion

ToWwwFormUrlencoded

Converts a collection object to application/x-www-form-urlencoded format.

vb
Public Function ToWwwFormUrlencoded(ByRef Obj As Object, Optional ByVal IsUrlEncode As Boolean = True) As String

Parameters:

ParameterTypeDescription
ObjObjectCollection object (cCollection or Scripting.Dictionary)
IsUrlEncodeBooleanWhether to URL-encode values (default True)

Example:

vb
Dim Dic As New Scripting.Dictionary
Dic.Add "username", "admin"
Dic.Add "password", "123456"

Dim FormData As String
FormData = VBMAN.ToolsHttp.ToWwwFormUrlencoded(Dic)
Debug.Print FormData  ' Output: username=admin&password=123456

FromWwwFormUrlencoded

Parses application/x-www-form-urlencoded format into collection object.

vb
Public Function FromWwwFormUrlencoded(ByVal Content As String, ByRef Obj As Object, Optional ByVal IsUrlDecode As Boolean = True) As Boolean

Example:

vb
Dim Dic As New Scripting.Dictionary
Dim Success As Boolean

Success = VBMAN.ToolsHttp.FromWwwFormUrlencoded("name=John&age=25", Dic)

Debug.Print Dic("name")  ' Output: John
Debug.Print Dic("age")   ' Output: 25

HTTP Method Mapping

MapMethod

Maps HTTP method name to internal index.

vb
Public Function MapMethod(Name As String) As Long

Mapping Table:

Method NameIndex
ANY0
POST1
GET2
PUT3
DELETE4
OPTIONS5

Example:

vb
Dim Index As Long
Index = VBMAN.ToolsHttp.MapMethod("GET")
Debug.Print Index  ' Output: 2

Index = VBMAN.ToolsHttp.MapMethod("POST")
Debug.Print Index  ' Output: 1

MapMethodName

Maps internal index to HTTP method name.

vb
Public Function MapMethodName(Index As Long) As String

Example:

vb
Dim MethodName As String
MethodName = VBMAN.ToolsHttp.MapMethodName(2)
Debug.Print MethodName  ' Output: GET

cHttpCrossDomain - HTTP Cross-Origin Configuration

Overview

Used to configure CORS (Cross-Origin Resource Sharing) response headers for HTTP server.

Properties

PropertyTypeDefaultDescription
EnableBooleanFalseWhether to enable cross-origin support
AllowOriginString"*"Allowed origin
AllowMethodsString"*"Allowed methods
AllowHeadersString"*"Allowed request headers
AllowCredentialsBooleanTrueWhether to allow credentials
MaxAgeLong86400000Preflight request cache time (milliseconds)

Usage

vb
' Enable cross-origin
VBMAN.HttpCrossDomain.Enable = True

' Custom configuration
With VBMAN.HttpCrossDomain
    .Enable = True
    .AllowOrigin = "https://example.com"
    .AllowMethods = "GET, POST, PUT, DELETE"
    .AllowHeaders = "Content-Type, Authorization"
    .AllowCredentials = True
End With

Usage in HttpServer

vb
Private Sub Server_OnBeforeRequest(Request As cHttpServerRequest)
    ' Automatically add cross-origin headers
    VBMAN.HttpCrossDomain.AddTo Request.Response.Headers
End Sub

Complete Example

vb
Private Sub HttpDemo()
    ' URL encoding
    Dim Url As String
    Url = "http://api.example.com/search?q=" & _
          VBMAN.ToolsHttp.UrlEncodeUtf8("VB6 Programming")
    Debug.Print Url
    
    ' Build query parameters
    Dim Params As New Scripting.Dictionary
    Params.Add "page", "1"
    Params.Add "size", "20"
    Params.Add "keyword", "VB6"
    
    Dim QueryString As String
    QueryString = VBMAN.ToolsHttp.MakeContent(Params)
    Url = "http://api.example.com/list?" & QueryString
    Debug.Print Url
    
    ' Parse query string
    Dim Parsed As New Scripting.Dictionary
    VBMAN.ToolsHttp.ParseContent QueryString, Parsed
    Debug.Print "Page: " & Parsed("page")
    Debug.Print "Size: " & Parsed("size")
    
    ' Parse HTTP Headers
    Dim Headers As New Scripting.Dictionary
    Dim HeaderText As String
    HeaderText = "Content-Type: application/json" & vbCrLf & _
                 "Authorization: Bearer xxx"
    VBMAN.ToolsHttp.ParseKeyValue HeaderText, Headers
End Sub

VB6 and LOGO copyright of Microsoft Corporation