Source: XMLOverHTTP.cpp

 

This C/C++ code performs the following steps:

  1. Creates an IXMLHTTPRequest object, pIXMLHTTPRequest.

  2. Uses the open method and the send method to request the contact information for "John Doe".

  3. Uses the responseXML property to retrieve the XML data returned from the server.

  4. Displays the XML data to the console.

C/C++ Source File (XMLOverHTTP.cpp)

#include <stdio.h>
#include <tchar.h>
#import <msxml6.dll>

// Macro that calls a COM method returning HRESULT value.
#define CHK_HR(stmt)        do { hr=(stmt); if (FAILED(hr)) goto CleanUp; } while(0)

// Macro that releases a COM object if not NULL.
#define SAFE_RELEASE(p)     do { if ((p)) { (p)->Release(); (p) = NULL; } } while(0)

void XMLOverHTTP()
{
    MSXML2::IXMLHTTPRequestPtr pIXMLHTTPRequest;
    MSXML2::IXMLDOMDocumentPtr pXMLDoc;
    BSTR bstrString = NULL;
    HRESULT hr = S_OK;

    try 
    {
        CHK_HR(pIXMLHTTPRequest.CreateInstance(__uuidof(MSXML2::XMLHTTP60), NULL, CLSCTX_INPROC_SERVER));
        CHK_HR(pIXMLHTTPRequest->open("GET", "https://localhost/sxh/contact.asp?SearchID=John Doe", _variant_t(VARIANT_FALSE)));
        CHK_HR(pIXMLHTTPRequest->send());

        pXMLDoc = pIXMLHTTPRequest->responseXML;
        bstrString = pXMLDoc->xml;
        if(bstrString)
        {
            printf("%S\n", bstrString);
        }
    } 
    catch (_com_error errorObject)
    {
        printf("Exception thrown, HRESULT: 0x%08x", errorObject.Error());
    }

CleanUp:
    SysFreeString(bstrString);
}

int _tmain(int argc, _TCHAR* argv[])
{
    HRESULT hr = CoInitialize(NULL);
    if(SUCCEEDED(hr))
    {
        XMLOverHTTP();
        CoUninitialize();
    }

    return 0;
}

To add the XMLOverHTTP source code to the project

  1. Create a new C++ source file. For detailed instructions on how to do this, see Set Up My Visual C++ Project. Name the file XMLOverHTTP.cpp.

  2. Copy the C/C++ source code above, and paste it into the source file you just created.

Next, we'll set up a virtual directory. You must complete this step before you build and run the application.