Source: XMLOverHTTP.cpp
This C/C++ code performs the following steps:
- Creates an
IXMLHTTPRequest
object,pIXMLHTTPRequest
. - Uses the
open
method and thesend
method to request the contact information for "John Doe". - Uses the
responseXML
property to retrieve the XML data returned from the server. - Displays the XML data to the console.
C/C++ Source File (XMLOverHTTP.cpp)
#include <stdio.h> #import <msxml5.dll> using namespace MSXML2; int main(int argc, char* argv[]) { CoInitialize(NULL); IXMLHTTPRequestPtr pIXMLHTTPRequest = NULL; IXMLDOMDocumentPtr pXMLDoc = NULL; BSTR bstrString = NULL; HRESULT hr; try { hr=pIXMLHTTPRequest.CreateInstance(__uuidof(XMLHTTP50)); SUCCEEDED(hr) ? 0 : throw hr; hr=pIXMLHTTPRequest->open("GET", "http://localhost/sxh/contact.asp?SearchID=John Doe", false); SUCCEEDED(hr) ? 0 : throw hr; hr=pIXMLHTTPRequest->send(); SUCCEEDED(hr) ? 0 : throw hr; pXMLDoc=pIXMLHTTPRequest->responseXML; bstrString=pXMLDoc->xml; if(bstrString) { printf("%S\n", bstrString); SysFreeString(bstrString); bstrString = NULL; } } catch (...) { printf("error: %x\n",hr); } if(bstrString) SysFreeString(bstrString); if (pIXMLHTTPRequest) pIXMLHTTPRequest.Release(); if (pXMLDoc) pXMLDoc.Release(); CoUninitialize(); return 0; }
To add the XMLOverHTTP source code to the project
- 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.
- 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.