CWebView::SetHeader Method  
 
BOOL SetHeader (
  LPCTSTR lpszHeaderName,  
  LPCTSTR lpszHeaderValue  
);

The SetHeader function sets a request header value to be included with subsequent navigations.

Parameters

lpszHeaderName
A null-terminated string which specifies the name of the request header value to set or change.
lpszHeaderValue
A null-terminated string which specifies the value of the header. If this value is NULL or an empty string, the request header will be removed.

Return Value

If the method succeeds, the return value is non-zero. If the method fails, the return value is zero. To get extended error information, call GetLastError.

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, 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.

Requirements

Minimum Desktop Platform: Windows 7 Service Pack 1
Minimum Server Platform: Windows Server 2008 R2 Service Pack 1
Header File: cstools12.h
Import Library: cshtmv12.lib
Unicode: Implemented as Unicode and ANSI versions

See Also