Microsoft HomeproductssearchsupportshopWrite Us   Microsoft Home
Magazine
 |  Community
 |  Workshop
 |  Tools & Samples
 |  Training
 |  Site Info

Workshop  |  Networking, Protocols & Data Formats

Retrieving HTTP Headers


This tutorial describes how to retrieve header information from HTTP requests by applications using Win32® Internet functions.

Requirements and Dependencies

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.

Implementation Steps

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.

Retrieving Headers Using a Constant

To use the HttpQueryInfo function to retrieve an HTTP header using a constant, follow these steps:

  1. Call HttpQueryInfo with a constant from the Attributes list, a NULL buffer, and the variable holding the buffer size set to zero. Also, if your application needs the data in a particular format, you can add a constant from the Modifiers list.
  2. If the requested HTTP header exists, the call to HttpQueryInfo should fail, GetLastError should return ERROR_INSUFFICIENT_BUFFER, and the variable passed for the lpdwBufferLength parameter should be set to the number of bytes required.
  3. Allocate a buffer with the number of bytes required.
  4. Retry the call to HttpQueryInfo.

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

Retrieving Headers Using HTTP_QUERY_CUSTOM

To use the HttpQueryInfo function to retrieve an HTTP header using HTTP_QUERY_CUSTOM, follow these steps:

  1. Allocate a buffer that is large enough to hold the string name of the HTTP header.
  2. Write the string name of the HTTP header into the buffer.
  3. Call HttpQueryInfo with HTTP_QUERY_CUSTOM, the buffer containing the string name of the HTTP header, and the variable holding the buffer size. Also, if your application needs the data in a particular format, you can add a constant from the Modifiers list.
  4. If the call to HttpQueryInfo fails and GetLastError returns ERROR_INSUFFICIENT_BUFFER, reallocate a buffer with the number of bytes required.
  5. Write the string name of the HTTP header into the buffer again.
  6. Retry the call to HttpQueryInfo.

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

Does this content meet your programming needs? Write us!

Back to topBack to top

© 1998 Microsoft Corporation. All rights reserved. Terms of use.

 

Magazine Home
Ask Jane
DHTML Dude
Extreme XML
For Starters
More or Hess
Servin' It Up
Site Lights
Web Men Talking
Member Community Home
Benefits: Freebies & Discounts
Benefits: Promote Your Site
Benefits: Connect with Your Peers
Benefits at a Glance
Online Special-Interest Groups
Your Membership
SBN Stores
Join Now
Workshop Home
Essentials
Content & Component Delivery
Component Development
Data Access & Databases
Design
DHTML, HTML & CSS
Extensible Markup Language (XML)
Languages & Development Tools
Messaging & Collaboration
Networking, Protocols & Data Formats
Reusing Browser Technology
Security & Cryptography
Server Technologies
Streaming & Interactive Media
Web Content Management
Workshop Index
Tools & Samples Home
Tools
Samples, Headers, Libs
Images
Sounds
Style Sheets
Web Fonts
Training Home
SBN Live Seminars
SBN Live Chats
Courses
Peer Support
CD-ROM Training
Books & Training Kits
Certification
SBN Home
New to SBN?
What's New on SBN
Site Map
Site Search
Glossary
Write Us
About This Site