The SetHeader method adds or updates a custom
request header that will be included with subsequent navigations.
Request header names are not case-sensitive, and any leading or
trailing whitespace is ignored. If the specified header value is NULL
or an empty string, the header will be removed from the current list
of custom headers. These changes only affect future requests; they do
not modify headers already sent for the current navigation.
Certain standard headers are
controlled by the underlying WebView2 control and may be ignored if
set programmatically. Examples include Host,
Referer, Origin, User-Agent,
and most authentication headers. Attempting to override these may
have no effect. For headers that can be set, the specified values
will be merged with the default request headers used by WebView2
when making requests. If you wish to change the User-Agent
header value, use the SetUserAgent method.
When defining custom request headers, it is recommended to use
names prefixed with X- (for example, X-Application-ID
or X-Request-Token). This convention helps distinguish
application-specific headers from those defined by the HTTP standard,
and reduces the likelihood of conflicts with headers that are managed
internally by WebView2 or intermediaries such as proxies and gateways.
While most modern systems will accept arbitrary header names,
following this convention improves compatibility and avoids unexpected
behavior.
If you need to clear all custom request headers that have been set,
call ClearHeaders with the WEBVIEW_HEADERS_REQUEST
option.