This tutorial describes how to retrieve header information from HTTP requests by applications using Win32® Internet functions.
Application developers who want to retrieve HTTP header information from requests made by a Win32 Internet function must have an understanding of C/C++ programming, a familiarity with Win32 programming, HTTP/1.1, and a familiarity with the Win32 Internet functions.
To compile programs using any of the Win32 Internet functions, make sure the Wininet.h header file is in the include directory and the Wininet.lib library file is in the library directory of the C/C++ compiler you are using.
There are two ways to retrieve the header information:
Using the constant associated with the HTTP header that your application needs is faster internally, but there might be HTTP headers that do not have a constant associated with them. For those cases, the method using the HTTP_QUERY_CUSTOM attribute flag is available.
Both methods use the HttpQueryInfo function. HttpQueryInfo takes the HINTERNET handle on which the HTTP request was made, one attribute, a buffer, a DWORD value containing the buffer size, and an index value. A modifier can also be added to the attribute passed to HttpQueryInfo to indicate what format the data should be returned in.
To use the HttpQueryInfo function to retrieve an HTTP header using a constant, follow these steps:
The following sample demonstrates a call to HttpQueryInfo using the HTTP_QUERY_RAW_HEADERS_CRLF constant, which is a special value that requests all of the returned HTTP headers.
LPVOID lpOutBuffer=NULL; DWORD dwSize = 0; retry: // This call will fail on the first pass, since no buffer is allocated. if(!HttpQueryInfo(hHttp,HTTP_QUERY_RAW_HEADERS_CRLF, (LPVOID)lpOutBuffer,&dwSize,NULL)) { if (GetLastError()==ERROR_HTTP_HEADER_NOT_FOUND) { // Code to handle the case where the header isn't available. return TRUE; } else { // Check for an insufficient buffer. if (GetLastError()==ERROR_INSUFFICIENT_BUFFER) { // Allocate the necessary buffer. lpOutBuffer = new char[dwSize]; // Retry the call. goto retry; } else { // Error handling code. return FALSE; } } }
To use the HttpQueryInfo function to retrieve an HTTP header using HTTP_QUERY_CUSTOM, follow these steps:
The following sample demonstrates a call to HttpQueryInfo using the HTTP_QUERY_CUSTOM constant to request the Content-Type HTTP header.
LPVOID lpOutBuffer; DWORD dwSize = 20; sprintf((LPSTR)lpOutBuffer,"Content-Type"); retry: if(!HttpQueryInfo(hHttp,HTTP_QUERY_CUSTOM, (LPVOID)lpOutBuffer,&dwSize,NULL)) { if (GetLastError()==ERROR_HTTP_HEADER_NOT_FOUND) { // Code to handle the case where the header isn't available. return TRUE; } else { // Check for an insufficient buffer. if (GetLastError()==ERROR_INSUFFICIENT_BUFFER) { // Allocate the necessary buffer. lpOutBuffer = new char[dwSize]; // Rewrite the header name in the buffer. sprintf((LPSTR)lpOutBuffer,"Content-Type"); // Retry the call. goto retry; } else { // Error handling code. return FALSE; } } }