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.
Public Function UrlEncode(ByRef strURL As String) As StringDescription:
- Uses GB2312 encoding
- Suitable for traditional encoding scenarios with Chinese characters
Example:
Dim Encoded As String
Encoded = VBMAN.ToolsHttp.UrlEncode("你好世界")
Debug.Print Encoded ' Output: %C4%E3%BA%C3%CA%C0%BD%E7UrlDecode
Decodes a GB2312 URL-encoded string.
Public Function UrlDecode(ByVal url As String) As StringExample:
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).
Public Function UrlEncodeUtf8(ByVal szInput As Variant) As StringDescription:
- Follows RFC3986 standard
- Safe characters:
A-Z a-z 0-9 - _ . ~ - Other characters are percent-encoded
- Supports multi-byte UTF-8 characters
Example:
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%26eUrlDecodeUtf8
Decodes a UTF-8 URL-encoded string.
Public Function UrlDecodeUtf8(ByVal url As String) As StringExample:
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.
Public Function AddToQueryString(ByVal url As String, ByVal QS As String) As StringDescription:
- Automatically determines if URL already has query parameters
- Automatically adds
?or&
Example:
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=JohnMakeContent
Builds a dictionary into query string format.
Public Function MakeContent(Dic As Scripting.Dictionary, Optional IsUrlEncode As Boolean = True) As StringParameters:
| Parameter | Type | Description |
|---|---|---|
Dic | Scripting.Dictionary | Dictionary to convert |
IsUrlEncode | Boolean | Whether to URL-encode values (default True) |
Example:
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=BeijingParseContent
Parses query string into dictionary.
Public Function ParseContent(Content As String, Obj As Scripting.Dictionary, Optional IsUrlDecode As Boolean = True) As BooleanParameters:
| Parameter | Type | Description |
|---|---|---|
Content | String | Query string |
Obj | Scripting.Dictionary | Dictionary to store results |
IsUrlDecode | Boolean | Whether to URL-decode (default True) |
Example:
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: 25ParseKeyValue
Parses key-value formatted string into dictionary (e.g., HTTP Headers).
Public Function ParseKeyValue(Content As String, Obj As Scripting.Dictionary) As BooleanDescription:
- Splits by lines
- Each line splits by first
:into key and value - Suitable for parsing HTTP Headers
Example:
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 token123Form URL-Encoded Conversion
ToWwwFormUrlencoded
Converts a collection object to application/x-www-form-urlencoded format.
Public Function ToWwwFormUrlencoded(ByRef Obj As Object, Optional ByVal IsUrlEncode As Boolean = True) As StringParameters:
| Parameter | Type | Description |
|---|---|---|
Obj | Object | Collection object (cCollection or Scripting.Dictionary) |
IsUrlEncode | Boolean | Whether to URL-encode values (default True) |
Example:
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=123456FromWwwFormUrlencoded
Parses application/x-www-form-urlencoded format into collection object.
Public Function FromWwwFormUrlencoded(ByVal Content As String, ByRef Obj As Object, Optional ByVal IsUrlDecode As Boolean = True) As BooleanExample:
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: 25HTTP Method Mapping
MapMethod
Maps HTTP method name to internal index.
Public Function MapMethod(Name As String) As LongMapping Table:
| Method Name | Index |
|---|---|
| ANY | 0 |
| POST | 1 |
| GET | 2 |
| PUT | 3 |
| DELETE | 4 |
| OPTIONS | 5 |
Example:
Dim Index As Long
Index = VBMAN.ToolsHttp.MapMethod("GET")
Debug.Print Index ' Output: 2
Index = VBMAN.ToolsHttp.MapMethod("POST")
Debug.Print Index ' Output: 1MapMethodName
Maps internal index to HTTP method name.
Public Function MapMethodName(Index As Long) As StringExample:
Dim MethodName As String
MethodName = VBMAN.ToolsHttp.MapMethodName(2)
Debug.Print MethodName ' Output: GETcHttpCrossDomain - HTTP Cross-Origin Configuration
Overview
Used to configure CORS (Cross-Origin Resource Sharing) response headers for HTTP server.
Properties
| Property | Type | Default | Description |
|---|---|---|---|
Enable | Boolean | False | Whether to enable cross-origin support |
AllowOrigin | String | "*" | Allowed origin |
AllowMethods | String | "*" | Allowed methods |
AllowHeaders | String | "*" | Allowed request headers |
AllowCredentials | Boolean | True | Whether to allow credentials |
MaxAge | Long | 86400000 | Preflight request cache time (milliseconds) |
Usage
' 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 WithUsage in HttpServer
Private Sub Server_OnBeforeRequest(Request As cHttpServerRequest)
' Automatically add cross-origin headers
VBMAN.HttpCrossDomain.AddTo Request.Response.Headers
End SubComplete Example
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