# 概述
VBA-Web由三个主要组件组成:
**WebClient** 执行请求并处理响应,并负责请求之间共享的功能,例如身份验证,代理配置和安全性。 它被设计为长期存在,维护请求之间的状态(例如,身份验证令牌)。
**WebRequest** 用于创建详细请求,包括自动格式化,查询字符串,标头,cookie等等。
**WebResponse** 包装http / cURL响应并包括自动解析的数据。
以下示例是VBA-Web模块的标准表单:
1. 一个长期存在的`WebClient`,用于设置所有请求共享的基本URL
2. 一个详细的`WebRequest`(设置`Resource`,`Method`和`Format`)
3. 包含响应,包括基于`Request.Format`自动转换的`Data`
~~~
'// 长期客户端,在请求之间维护状态
Private ClientInstance As WebClient
Public Property Get Client() As WebClient
If ClientInstance Is Nothing Then
'// 设置所有请求共享的基本URL
Set ClientInstance = New WebClient
ClientInstance.BaseUrl = "https://www.example.com/api/"
End If
Set Client = ClientInstance
End Property
Public Function GetProjects() As Collection
'// 使用GET和json(用于请求和响应)
Dim Request As New WebRequest
Request.Resource = "projects"
Request.Method = WebMethod.HttpGet
Request.Format = WebFormat.Json
Dim Response As WebResponse
Set Response = Client.Execute(Request)
'// -> GET https://www.example.com/api/projects
'//
'// <- HTTP/1.1 200 OK
'// ...
'// {"data":[{"id":1,"name":"Project 1"},{"id":2,"name":"Project 2"}]}
If Response.StatusCode <> WebStatus.Ok Then
Err.Raise Response.StatusCode, "GetProjects", Response.Content
Else
'// 响应由Request.Format自动转换为Dictionary / Collection
Set GetProjects = Response.Data("data")
End If
End Function
~~~
- README
- 指南
- 概述
- GET Request
- WebRequest
- 属性
- Resource
- Method
- Body
- Format
- RequestFormat
- ResponseFormat
- CustomRequestFormat
- CustomResponseFormat
- ContentType
- Accept
- ContentLength
- FormattedResource
- Cookies
- Headers
- QuerystringParams
- UrlSegments
- 方法
- AddHeader
- SetHeader
- AddUrlSegment
- AddQuerystringParam
- AddCookie
- AddBodyParameter
- CreateFromOptions
- WebClient
- 属性
- BaseUrl
- Authenticator
- TimeoutMs
- ProxyServer
- ProxyBypassList
- ProxyUsername
- ProxyPassword
- EnableAutoProxy
- Insecure
- FollowRedirects
- 方法
- Execute
- GetJson
- PostJson
- SetProxy
- GetFullUrl
- WebResponse
- 属性
- StatusCode
- StatusDescription
- Content
- Data
- Body
- Headers
- Cookies
- 方法
- Update
- WebHelpers
- 属性
- WebStatusCode
- WebMethod
- WebFormat
- UrlEncodingMode
- EnableLogging
- 方法
- LogDebug
- LogWarning
- LogError
- LogRequest
- LogResponse
- Obfuscate
- ParseJson
- ConvertToJson
- ParseUrlEncoded
- ConvertToUrlEncoded
- ParseXml
- ConvertToXml
- ParseByFormat
- ConvertToFormat
- UrlEncode
- UrlDecode
- Base64Encode
- Base64Decode
- RegisterConverter
- JoinUrl
- UrlParts
- CloneDictionary
- CloneCollection
- CreateKeyValue
- FindInKeyValues
- AddOrReplaceInKeyValues
- FormatToMediaType
- MethodToName
- HMACSHA1
- HMACSHA256
- MD5
- CreateNonce
- IWebAuthenticator
- 方法
- BeforeExecute
- AfterExecute
- PrepareHttp
- PrepareCurl
- WebAsyncWrapper
- 属性
- Client
- 方法
- ExecuteAsync
- 范例
- Salesforce网站
- Google APIs
- Todoist API
- 其他主题
- 调试
- 授权
- 实现自己的IWebAuthenticator
- Url编码