VBMAN API Call Example
Overview
This example demonstrates how to use the VBMAN framework to call external API interfaces, including HTTP GET/POST requests, parameter settings, response parsing, and other functionalities.
Project Structure
Download source [ Note:To bin regedits DLL file ]
Please go to the home page to download the VBMAN project, decompress it, and open it with all DEMO projects.
ApiCall/
├── src/ # Source code directory
│ ├── Form1.frm # Main form
│ └── ApiCall.vbp # Project file
└── dist/ # Build output directory
└── ApiCall.exe # Executable fileCore Code Analysis
1. API Call Example
vb
'Create HTTP client
Dim http As New cHttpClient
'Example of GET request (one-line example, using the global VBMAN object)
MsgBox VBMAN.HttpClient.SendGet("http://api.example.com/users").ReturnText()
'Example of POST request
With new cHttpClient
Const Url as String = "http://api.example.com/users/list"
'Set request headers, e.g., for interfaces that require a token, set it here
.RequestHeader.Add "Authorization", "Bearer " & token
'If parameters need to be appended to the URL, construct them this way; they will be automatically concatenated internally
.RequestDataQuery.Add "time", Now()
.RequestDataQuery.Add "version", 1.0.0
'Choose one of the following data formats
'1. If the interface requires form data format
'Set POST data as form data
.RequestDataBody.Add "search","woeoio"
.RequestDataBody.Add "sort","ASC"
'Send the request. In this case, parameter 2 is not needed; the form will be automatically built and urlencoded internally
.SendPost Url
'2. If the interface requires JSON data format
'Set POST data by constructing a JSON object first
With new cJson
.Item("search") = "woeoio"
.Item("sort") = "ASC"
'Next, convert the object to a JSON string for the variable to be used below
Const PostData as String = .Encode()
End With
'Send the request, set the content type to JSON (default is form), and pass the JSON string as parameter 2
.SetRequestContentType JsonString
.SendPost Url, PostData
'Process the response. If an error occurs, this line will not be reached, so use On Error for error handling
With .ReturnJson() '.ReturnBody returns binary, .ReturnText returns text
if .Root("code") = 0 then
dim i as long, x as variant
'Here you can process the data returned by the backend, e.g., iterate through the recordset. Arrays start from 1.
for i = 1 to .Root("data")("count")
List1.AddItem .Root("data")("rs")(i)("username")
next
'You can also use for each for a more concise approach
for each x in .Root("data")("rs")
list2.AddItem x("username")
next
Else
MsgBox .Root("msg") 'Display the error message returned by the backend
End if
End With
End With2. File Upload Example
vb
'File upload
With http
.FormData.Add "file", "c:\test.jpg"
.FormData.Add "name", "Test Image"
.Post "http://api.example.com/upload"
End With3. Batch Request Example
vb
'Concurrent requests
With http
.AsyncMode = True 'Enable async mode
'Initiate multiple requests
.Get "http://api1.example.com"
.Get "http://api2.example.com"
.Get "http://api3.example.com"
'Wait for all requests to complete
.WaitAll
'Get responses
For Each res In .AsyncResponses
Debug.Print res.url, res.status
Next
End WithFeature Description
HTTP Request Features
- Support for GET/POST/PUT/DELETE methods
- Form submission (application/x-www-form-urlencoded)
- File upload (multipart/form-data)
- JSON data (application/json)
- Asynchronous requests
Request Configuration
- Request header settings
- Cookie management
- Timeout settings
- Proxy settings
- SSL/TLS support
Response Handling
- Status code checking
- Response header parsing
- JSON parsing
- File downloading
- Character set handling
Technical Points
- HTTP protocol implementation
- HTTPS secure communication
- Asynchronous programming model
- Error handling mechanism
Use Cases
- Third-party API integration
- Microservice communication
- File upload and download
- Data synchronization
Extension Suggestions
- Add request retry mechanism
- Implement request queue
- Add response caching
- Implement request rate limiting
- Support WebSocket