CHttpClient::AddHeaders Method  
 
BOOL AddHeaders(
  LPCTSTR lpszHeaderList  
);

The AddHeaders method adds one or more request headers for the current client session.

Parameters

lpszHeaderList
A null-terminated string which specifies one or more header values which should be set for the current client session. This parameter cannot be NULL.

Return Value

If the method succeeds, the return value is non-zero. If a connection has not been established with a server, or the header list contains invalid values, this method returns a value of zero. To get extended error information, call the GetLastError method.

Remarks

The AddHeaders method enables your application to set one or more header values by providing a list of name/value pairs separated by a colon. Multiple header values may be provided by separating them with a newline character. This method is similar to calling the SetHeader method for each value. When the list of header values is parsed, extraneous whitespace is ignored; however, if the header list contains invalid text (for example, a missing colon separating a header name from its value) the method will fail and an error will be returned.

Exercise caution when providing a header list created directly from user input, such as a list of values input using a textbox control. Any header values which have been previously set by your application can be overridden by this method and may yield unpredictable results. If the service you are using requires a custom authorization header, such as an API token or other user credentials, allowing users to directly modify request header values this way can present a security risk.

Example

// Define a list of header values which should be included with the
// request submitted to the server
LPCTSTR lpszHeaderList = _T("X-API-Token: 99d2fe39-0246-4efa-98e0-4d775579fa5d\n") \
                         _T("X-API-UserId: someuser@domain.tld\n") \
                         _T("X-API-Version: 1.1\n")

if (!pClient->AddHeaders(lpszHeaderList))
{
    // Unable to add the headers for this client session
    return;
}

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: cshtpv12.lib
Unicode: Implemented as Unicode and ANSI versions

See Also