The response headers exposed by this method correspond to the
most recent completed navigation for the specified browser instance.
Callers typically use this method to start an enumeration and then
call GetNextHeader repeatedly until it returns FALSE to
indicate there are no more headers to enumerate.
Header names are case-insensitive by definition. This method
will usually return header names as lower-case values, but
applications should never depend on the casing and should compare
names case-insensitively. The header names and values are returned
exactly as enumerated internally by the WebView2 control.
Some response headers can appear multiple times in a message. For
list-valued headers (those defined by the HTTP specifications to be
comma-joinable), the implementation may return a single entry with a
comma-separated value (e.g., Accept-Language,
Cache-Control, etc.). Other headers must not be combined;
for example, each Set-Cookie header is returned as a
separate name/value pair and will be seen multiple times during
enumeration, each with a single cookie value. Code that depends on
specific headers should be prepared for either a single combined
value or multiple discrete occurrences of the same header name,
depending on the header's semantics.
Enumeration reflects a snapshot of the headers for that response.
Beginning a new navigation or calling ClearHeaders
invalidates the current enumeration. Only one header enumeration can
be active per browser instance at a time. The enumeration state is not
thread-safe, and any access to the response headers should be
synchronized across different threads.
If the provided buffers are too small, the header names and values
will be truncated to the available space specified by the maximum
length parameters. In most cases, a header name is not going to exceed
256 characters and a header value will normally not exceed 5120
characters. Most header values are relatively short, but some headers
such as Set-Cookie can return much longer values.
This method cannot be called while the browser is in the process
of navigating to a new location. It will also fail if the current
document is a local file or if the document text was set dynamically
using SetDocumentText. There are no response headers
available in a local context.
TCHAR szName[WEBVIEW_MAX_HEADER_NAME];
TCHAR szValue[WEBVIEW_MAX_HEADER_VALUE];
if (pWebView->GetFirstHeader(szName, _countof(szName),
szValue, _countof(szValue)))
{
do
{
_tprintf(_T("%s: %s\n"), szName, szValue);
}
while (pWebView->GetNextHeader(szName, _countof(szName),
szValue, _countof(szValue)));
}
else
{
DWORD dwError = pWebView->GetLastError();
_tprintf(_T("No headers available (error 0x%lx)\n"), dwError);
}