| title | Header |
|---|---|
| parent | WebView2 Package |
| nav_order | 15 |
The WebView2Header class represents a single HTTP header information, containing header name and header value.
| Property | Value |
|---|---|
| Class Name | WebView2Header |
| COM Creatable | No |
Public Name As StringThe name of the HTTP header.
Type: String
Example:
Dim header As WebView2Header
header.Name = "Content-Type"Public Value As StringThe value of the HTTP header.
Type: String
Example:
Dim header As WebView2Header
header.Value = "application/json"Public Sub New(ByVal Name As String, ByVal Value As String)Creates a new WebView2Header object.
Parameters:
Name- Header nameValue- Header value
Example:
Dim header As New WebView2Header("Content-Type", "text/html")
Debug.Print header.Name & ": " & header.Value
' Output: Content-Type: text/html' Create single header object
Dim authHeader As New WebView2Header("Authorization", "Bearer token123")
Debug.Print authHeader.Name & ": " & authHeader.Value
' Create multiple headers
Dim headers() As WebView2Header
ReDim headers(2)
Set headers(0) = New WebView2Header("Content-Type", "application/json")
Set headers(1) = New WebView2Header("Accept", "application/json")
Set headers(2) = New WebView2Header("User-Agent", "MyApp/1.0")
Dim i As Long
For i = LBound(headers) To UBound(headers)
Debug.Print headers(i).Name & ": " & headers(i).Value
NextPrivate Sub WebView21_NavigationStarting(ByVal Uri As String, _
ByVal IsUserInitiated As Boolean, ByVal IsRedirected As Boolean, _
ByVal RequestHeaders As WebView2RequestHeaders, ByRef Cancel As Boolean)
Dim header As WebView2Header
For Each header In RequestHeaders
Debug.Print "Name: " & header.Name
Debug.Print "Value: " & header.Value
Debug.Print "---"
Next
End SubPrivate Function ValidateHeader(ByVal header As WebView2Header) As Boolean
' Check if header name is valid
If Len(header.Name) = 0 Then
Debug.Print "Error: Header name is empty"
ValidateHeader = False
Exit Function
End If
' Check if header name contains illegal characters
Dim invalidChars As String
invalidChars = "()<>@,;:\""/[]?={} \t"
Dim i As Long
For i = 1 To Len(invalidChars)
If InStr(header.Name, Mid(invalidChars, i, 1)) > 0 Then
Debug.Print "Error: Header name contains illegal characters"
ValidateHeader = False
Exit Function
End If
Next
ValidateHeader = True
End Function
' Usage example
Dim testHeader As New WebView2Header("Invalid-Name()", "value")
If Not ValidateHeader(testHeader) Then
Debug.Print "Header validation failed"
End If' Convert header object to string
Function HeaderToString(ByVal header As WebView2Header) As String
HeaderToString = header.Name & ": " & header.Value
End Function
' Usage example
Dim header As New WebView2Header("Content-Type", "application/json")
Debug.Print HeaderToString(header)
' Output: Content-Type: application/json
' Convert header object array to string
Function HeadersToString(headers() As WebView2Header) As String
Dim result As String
Dim i As Long
For i = LBound(headers) To UBound(headers)
result = result & headers(i).Name & ": " & headers(i).Value & vbCrLf
Next
HeadersToString = result
End Function' Filter specific headers
Private Sub FilterHeaders(ByVal RequestHeaders As WebView2RequestHeaders)
Dim header As WebView2Header
For Each header In RequestHeaders
Select Case LCase(header.Name)
Case "authorization"
Debug.Print "Found authorization header: " & Left(header.Value, 10) & "..."
Case "cookie"
Debug.Print "Found Cookie header"
Case "user-agent"
Debug.Print "User Agent: " & header.Value
End Select
Next
End Sub' Clone header object
Function CloneHeader(ByVal header As WebView2Header) As WebView2Header
Set CloneHeader = New WebView2Header(header.Name, header.Value)
End Function
' Clone header object array
Function CloneHeaders(headers() As WebView2Header) As WebView2Header()
Dim result() As WebView2Header
ReDim result(UBound(headers))
Dim i As Long
For i = LBound(headers) To UBound(headers)
Set result(i) = CloneHeader(headers(i))
Next
CloneHeaders = result
End Function
' Usage example
Dim originalHeader As New WebView2Header("X-Custom", "value1")
Dim clonedHeader As WebView2Header
Set clonedHeader = CloneHeader(originalHeader)
' Modifying clone doesn't affect original object
clonedHeader.Value = "value2"
Debug.Print originalHeader.Value ' Output: value1
Debug.Print clonedHeader.Value ' Output: value2' Compare two header objects
Function HeadersEqual(ByVal header1 As WebView2Header, _
ByVal header2 As WebView2Header) As Boolean
HeadersEqual = (LCase(header1.Name) = LCase(header2.Name) And _
header1.Value = header2.Value)
End Function
' Usage example
Dim header1 As New WebView2Header("Content-Type", "application/json")
Dim header2 As New WebView2Header("Content-Type", "application/json")
Dim header3 As New WebView2Header("content-type", "application/json") ' Different case
Debug.Print HeadersEqual(header1, header2) ' Output: True
Debug.Print HeadersEqual(header1, header3) ' Output: True (case-insensitive name)' Find header
Function FindHeader(headers() As WebView2Header, ByVal name As String) As WebView2Header
Dim i As Long
For i = LBound(headers) To UBound(headers)
If LCase(headers(i).Name) = LCase(name) Then
Set FindHeader = headers(i)
Exit Function
End If
Next
Set FindHeader = Nothing
End Function
' Add header (if doesn't exist)
Sub AddHeaderIfNotExists(headers() As WebView2Header, _
ByVal name As String, ByVal value As String)
If FindHeader(headers, name) Is Nothing Then
Dim newSize As Long
newSize = UBound(headers) + 1
ReDim Preserve headers(newSize)
Set headers(newSize) = New WebView2Header(name, value)
End If
End Sub
' Usage example
Dim headers() As WebView2Header
ReDim headers(0)
Set headers(0) = New WebView2Header("Content-Type", "text/html")
AddHeaderIfNotExists headers, "Content-Type", "application/json"
AddHeaderIfNotExists headers, "Authorization", "Bearer token"
Debug.Print "Header count: " & UBound(headers) + 1 ' Output: 2Dim userAgent As New WebView2Header("User-Agent", "MyApp/1.0")
Dim accept As New WebView2Header("Accept", "application/json")
Dim contentType As New WebView2Header("Content-Type", "application/json")
Dim authorization As New WebView2Header("Authorization", "Bearer token123")
Dim cookie As New WebView2Header("Cookie", "session_id=abc123")Dim contentType As New WebView2Header("Content-Type", "text/html; charset=utf-8")
Dim contentLength As New WebView2Header("Content-Length", "1024")
Dim cacheControl As New WebView2Header("Cache-Control", "no-cache")
Dim setCookie As New WebView2Header("Set-Cookie", "session_id=xyz789; Path=/; HttpOnly")
Dim server As New WebView2Header("Server", "MyServer/1.0")Dim xContentTypeOptions As New WebView2Header("X-Content-Type-Options", "nosniff")
Dim xFrameOptions As New WebView2Header("X-Frame-Options", "DENY")
Dim xXssProtection As New WebView2Header("X-XSS-Protection", "1; mode=block")
Dim strictTransportSecurity As New WebView2Header("Strict-Transport-Security", "max-age=31536000")Dim accessControlAllowOrigin As New WebView2Header("Access-Control-Allow-Origin", "*")
Dim accessControlAllowMethods As New WebView2Header("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE")
Dim accessControlAllowHeaders As New WebView2Header("Access-Control-Allow-Headers", "Content-Type, Authorization")-
Case Sensitivity: HTTP header names are case-insensitive, use
LCaseorUCasefunctions when comparing. -
Space Handling: Spaces in header names and values are significant, don't trim them arbitrarily.
-
Multi-value Headers: Some headers may have multiple values (such as
Set-Cookie),WebView2Headeronly stores a single value, multiple values need multipleWebView2Headerobjects. -
Special Characters: Header names should avoid containing special characters, some characters are illegal.
-
Length Limits: HTTP headers have length limits (usually 8KB or more), overly long headers may cause issues.
-
Encoding Issues: Header values are typically ASCII characters, non-ASCII characters may need encoding.
-
Read-only Properties:
WebView2Headerobjects obtained inWebResourceRequestedevent are read-only, modifications won't affect the actual request. To modify headers, useRequestHeadersorResponseHeadersobject methods. -
Object Lifecycle:
WebView2Headerobjects are typically created and managed byWebView2HeadersCollection, no need for manual creation and destruction.