cHttpClient Component Overview
Introduction
cHttpClient is an HTTP client component based on WinHTTP that provides complete HTTP request functionality, supporting sync/async requests, chainable calls, JSON processing, Cookie management, and more. Also provides cSSEClient for SSE (Server-Sent Events) real-time message reception.
Features
| Feature | Description |
|---|---|
| HTTP Methods | Supports GET/POST/PUT/DELETE/OPTIONS |
| Data Formats | JSON, Form-UrlEncoded, Text auto-processing |
| Sync/Async | Supports sync and async request modes |
| Chainable API | Fluent API design with chainable operations |
| Cookie Management | Auto parse and set Cookies |
| SSL Support | Auto ignore SSL certificate errors |
| Encoding | Supports UTF-8 encoding auto-conversion |
| Debug Mode | Provides detailed request/response debug info |
| SSE Support | Independent SSE client for real-time messages |
Quick Start
HTTP GET Request
vb
Dim http As New cHttpClient
Dim result As String
' Simple GET request
result = http.SendGet("https://api.example.com/users").ReturnText()
' GET request with query parameters
result = http.SendGet("https://api.example.com/users") _
.AddQueryParam("page", "1") _
.AddQueryParam("limit", "10") _
.ReturnText()HTTP POST Request (JSON)
vb
Dim http As New cHttpClient
Dim json As cJson
Set json = New cJson
json.AddItem "name", "John"
json.AddItem "age", 25
Set http.RequestDataJson = json
Dim response As cJson
Set response = http.SendPost("https://api.example.com/users").ReturnJson()HTTP POST Request (Form)
vb
Dim http As New cHttpClient
http.RequestDataForm("username") = "admin"
http.RequestDataForm("password") = "123456"
Dim result As String
result = http.SendPost("https://api.example.com/login").ReturnText()Async Request
vb
Private WithEvents HttpClient As cHttpClient
Private Sub StartAsyncRequest()
Set HttpClient = New cHttpClient
HttpClient.Async(True).SendGet("https://api.example.com/data")
End Sub
Private Sub HttpClient_OnResponseFinished()
Debug.Print "Response complete: " & HttpClient.ReturnText()
End Sub
Private Sub HttpClient_OnError(ByVal ErrorNumber As Long, ByVal ErrorDescription As String)
Debug.Print "Error: " & ErrorDescription
End SubSSE (Server-Sent Events)
vb
Private WithEvents SSE As cSSEClient
Private Sub InitializeSSE()
Set SSE = New cSSEClient
SSE.AutoReconnect = True
SSE.ReconnectInterval = 3000 ' 3 second reconnect interval
SSE.MaxReconnectAttempts = 10
Call SSE.Connect("https://api.example.com/events")
End Sub
Private Sub SSE_OnOpen()
Debug.Print "SSE connection established"
End Sub
Private Sub SSE_OnMessage(EventName As String, Data As String, Id As String)
Debug.Print "Received message: " & EventName & " = " & Data
End Sub
Private Sub SSE_OnError(Description As String, ErrorNumber As Long)
Debug.Print "SSE error: " & Description
End Sub
Private Sub SSE_OnClose()
Debug.Print "SSE connection closed"
End SubReferences
Microsoft WinHTTP Services, version 5.1Microsoft Scripting RuntimecJson.clscTimer.cls(required for SSE)
File Structure
| File | Description |
|---|---|
cHttpClient.cls | HTTP client main class |
cSSEClient.cls | SSE client implementation |
mSSEDemo.bas | SSE usage example |
SSE_Client_使用文档.md | SSE detailed documentation |
Last Updated: 2026-05-17