If the method succeeds, the return value is the length of the
header value, not including the terminating null. If the method fails,
the return value is zero. To get extended error information, call
GetLastError.
The GetHeader method retrieves the value of a
specific HTTP response header returned by the server. Header names are
matched in a case-insensitive manner, with leading and trailing
whitespace automatically trimmed before comparison. If multiple values
are present for the specified header name, the behavior depends on the
type of header:
- For headers that must not be combined (such as
Set-Cookie
and Proxy-Authenticate), only the first value encountered
is returned.
- For all other headers, multiple values are joined together and
returned as a single comma-separated string, as defined by the HTTP
specification.
For headers such as Set-Cookie, which may legitimately
occur multiple times with distinct values, GetHeader will
only return the first occurrence. If your application needs to process
each value individually, you should use GetFirstHeader and
GetNextHeader to enumerate all response headers rather than
relying on this method.
If the lpszHeaderValue parameter is NULL, the method
returns the number of characters required to store the complete header
value, excluding the terminating null. This enables applications to
dynamically allocate a buffer of the correct size before retrieving
the actual value.
Response headers are only available after navigation has completed.
Calling this method while a navigation is still in progress, or when
displaying local file or data content, will fail.
The maximum recommended length for a header name is
WEBVIEW_MAX_HEADER_NAME (256 characters) and for a header
value is WEBVIEW_MAX_HEADER_VALUE (5120 characters). These
limits are intended as practical guidelines, not absolute restrictions.
INT nLength = WebViewGetHeader(hWndWebView, _T("Content-Type"), NULL, 0);
if (nLength <= 0)
{
_tprintf(TEXT("Unable to get Content-Type header (error 0x%lx)\n"), WebViewGetLastError());
return;
}
LPTSTR lpszValue = (LPTSTR)malloc((nLength + 1) * sizeof(TCHAR));
if (!lpszValue)
{
_tprintf(_T("Out of memory\n"));
return;
}
if (WebViewGetHeader(hWndWebView, _T("Content-Type"), lpszValue, nLength + 1) > 0)
{
_tprintf(_T("Content-Type: %s\n"), lpszValue);
}
else
{
_tprintf(_T("Failed to retrieve Content-Type (error 0x%lx)\n"), WebViewGetLastError());
}
free(lpszValue);