SetHeader Method  
 

Set the value of a request header field.

Syntax

object.SetHeader HeaderField, HeaderValue

Parameters

HeaderField
A string that specifies the name of the request header field.
HeaderValue
A string that specifies the value associated with the given header field. If this argument is set to an empty string, the field is deleted from the request header.

Return Value

This method returns a boolean value. If the method succeeds, it will return True. If the header field cannot be created or modified, it will return False.

Remarks

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, set the UserAgent property.

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 the ClearHeaders method.

See Also

ClearHeaders Method, GetFirstHeader Method, GetHeader Method, GetNextHeader Method,