| title | RequestHeaders |
|---|---|
| parent | WebView2 Package |
| nav_order | 12 |
The WebView2RequestHeaders class is used to manage HTTP request header information.
| Property | Value |
|---|---|
| Class Name | WebView2RequestHeaders |
| COM Creatable | No |
Public Function GetHeader(ByVal name As String) As StringGets the value of the request header with the specified name.
Parameters:
name- Request header name
Return Value: The value string of the request header
Example:
Private Sub WebView21_NavigationStarting(ByVal Uri As String, _
ByVal IsUserInitiated As Boolean, ByVal IsRedirected As Boolean, _
ByVal RequestHeaders As WebView2RequestHeaders, ByRef Cancel As Boolean)
Dim userAgent As String
userAgent = RequestHeaders.GetHeader("User-Agent")
Debug.Print "User-Agent: " & userAgent
End SubPublic Function Contains(ByVal name As String) As BooleanChecks if a request header with the specified name exists.
Parameters:
name- Request header name
Return Value: Returns True if it contains, otherwise returns False
Example:
If RequestHeaders.Contains("Authorization") Then
Debug.Print "Request contains Authorization header"
End IfPublic Sub AppendHeader(ByVal name As String, ByVal value As String)Sets the value of the request header (replaces if it already exists).
Parameters:
name- Request header namevalue- Request header value
Example:
' Add custom User-Agent
RequestHeaders.AppendHeader "User-Agent", "MyApp/1.0"
' Add Authorization header
RequestHeaders.AppendHeader "Authorization", "Bearer token123"Public Sub RemoveHeader(ByVal name As String)Removes the request header with the specified name.
Parameters:
name- Request header name
Example:
RequestHeaders.RemoveHeader("Cookie")Public Function GetHeaders(ByVal name As String) As WebView2HeadersCollectionGets all values of the request header with the specified name (there may be multiple headers with the same name).
Parameters:
name- Request header name
Return Value: WebView2HeadersCollection collection object
Public Function _NewEnum() As WebView2HeadersCollectionSupports For Each to enumerate all request headers.
Example:
Private 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 header.Name & ": " & header.Value
Next
End SubPrivate Sub WebView21_NavigationStarting(ByVal Uri As String, _
ByVal IsUserInitiated As Boolean, ByVal IsRedirected As Boolean, _
ByVal RequestHeaders As WebView2RequestHeaders, ByRef Cancel As Boolean)
Debug.Print "Navigating to: " & Uri
Debug.Print "Request headers:"
Dim header As WebView2Header
For Each header In RequestHeaders
Debug.Print " " & header.Name & ": " & header.Value
Next
End SubPrivate Sub WebView21_NavigationStarting(ByVal Uri As String, _
ByVal IsUserInitiated As Boolean, ByVal IsRedirected As Boolean, _
ByVal RequestHeaders As WebView2RequestHeaders, ByRef Cancel As Boolean)
' Add custom request header
RequestHeaders.AppendHeader "X-Custom-Header", "CustomValue"
' Replace User-Agent
RequestHeaders.AppendHeader "User-Agent", "MyApp/2.0"
End SubPrivate Sub WebView21_NavigationStarting(ByVal Uri As String, _
ByVal IsUserInitiated As Boolean, ByVal IsRedirected As Boolean, _
ByVal RequestHeaders As WebView2RequestHeaders, ByRef Cancel As Boolean)
' Intercept requests missing specific authentication header
If Not RequestHeaders.Contains("Authorization") Then
Debug.Print "Missing Authorization header, cancel navigation"
Cancel = True
End If
End Sub-
Modification Timing: Request header modifications can only be done in the
NavigationStartingevent; modifications at other times are invalid. -
Case Sensitivity: Request header names are case-insensitive, for example, "Content-Type" and "content-type" are considered the same.
-
Special Request Headers: Some request headers (such as
Host,Content-Length) are automatically managed by WebView2, manual modification may be invalid. -
Security: When handling sensitive information (such as
Authorization,Cookie), pay attention to security to avoid leaks. -
Encoding Issues: Request header values should comply with HTTP specifications, some special characters may need encoding.