CWebView::GetHeader Method  
 
INT GetHeader(
  LPCTSTR lpszHeaderName,  
  LPCTSTR lpszHeaderValue,  
  INT nMaxLength  
);

The GetHeader method returns the value of a specific response header by name.

Parameters

lpszHeaderName
A null-terminated string which specifies the response header name. This value is not case-sensitive but must match the complete header name. This parameter cannot be NULL.
lpszHeaderValue
A string buffer which will contain the value of the specified header. If this parameter is NULL then nothing will be copied but the method will return the length of the header value, not including the terminating null.
nMaxLength
The maximum number of characters which may be copied into the string buffer, including the terminating null. If the lpszHeaderValue parameter is NULL this value should be zero.

Return Value

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.

Remarks

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.

Example

 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);

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