Setting Multiple Headers for HttpSendRequest
HttpSendRequest is most commonly used WinAPI from WinInet class for sending a http Requests in Win32 programming.
Syntax is
BOOL HttpSendRequest(
_In_ HINTERNET hRequest,
_In_ LPCTSTR lpszHeaders,
_In_ DWORD dwHeadersLength,
_In_ LPVOID lpOptional,
_In_ DWORD dwOptionalLength
);
Parameters :
lpszHeaders [in] : Headers for the request.
But in most of the requirements the request might have multiple headers, like:
“Content-Type: application/x-www-form-
urlencoded”
“Content-Type: application/xml”
or customized headers like :
“sso_token :asff67afc4”
“staus_temp : proceed”
In these cases we can set multiple headers for a particular request by using WinAPI HttpAddRequestHeaders from WinInet class
BOOL HttpAddRequestHeaders(
_In_ HINTERNET hRequest,
_In_ LPCTSTR lpszHeaders,
_In_ DWORD dwHeadersLength,
_In_ DWORD dwModifiers
);
Parameters:
hRequest [in] : A handle returned by a call to the HttpOpenRequest function.
lpszHeaders [in] : Headers for the request
dwHeadersLength [in] : Length of the headers added in lpszHeaders parameter
Example:
BOOL CWinInetMgr::LogIn(CString& userID, Cstring& strStToken, CString& strToken, CString& strError)
{
//Form the command to send the request to login
CString strCmd = _T(“/signIn/xml”);
CString strData;
strData.Format(_T(“<?xml version=\”1.0\” encoding=\”UTF-8\”
?><username>%s</username>”),userID);
DWORD dwDataLen = strData.GetLength();
CString strCmdURL = m_strURL + strCmd;
m_hHttpFile = HttpOpenRequest( m_hConnect, dwDataLen ?
_T(“POST”) : _T(“GET”),strCmdURL, HTTP_VERSION,
NULL,NULL, m_dwHttpFlag, 0 );
if( NULL == m_hHttpFile ) {
strError = _T(“Error in Server Communication.”);
return FALSE;
}
// Setting the headers for Login command
CString strStHeader;
strStHeader.Format(_T(“token: %s”), strToken);
HttpAddRequestHeaders(m_hHttpFile, strStHeader,
strStHeader.GetLength(),NULL);
CString strTgtHeader;
strTgtHeader.Format(_T(“tgt_token: %s”), strStToken);
HttpAddRequestHeaders(m_hHttpFile, strTgtHeader,
strTgtHeader.GetLength(), NULL);
CString strHeader = _T(“Content-Type: application/xml”);
BOOL bResult = FALSE;
bResult = SendHttpRequest( strData, strHeader, dwDataLen,
strError );
return bResult ;
}
References:
MSDN : HttpSendRequest
MSDN : HttpAddRequestHeaders