IXMLHTTPRequest

MSXML 5.0 SDK

Microsoft XML Core Services (MSXML) 5.0 for Microsoft Office - XML Reference

IXMLHTTPRequest

Provides client-side protocol support for communication with HTTP servers.

[Script]

Example

The following Microsoft® JScript® example posts a DOMDocument containing order information to an Active Server Pages (ASP) page on a server and returns the result as a new XML document.

<script language="JScript">
  function PostOrder(xmldoc)
  {    
    var xmlhttp = new ActiveXObject("Msxml2.XMLHTTP.5.0");
    xmlhttp.Open("POST", "http://myserver/orders/processorder.asp", false);
    xmlhttp.Send(xmldoc);
    return xmlhttp.responseXML;
  }
</script>

The ASP page on the server loads the posted XML document, processes the order, and builds a resulting XML document.

<%@ language=javascript %>
<% 
   Response.Expires = -1000;
   // Load the posted XML document.
   var doc = Server.CreateObject("Msxml2.DOMDocument.5.0");
   doc.load(Request);
if (xmlDoc.parseError.errorCode <> 0) {
   var myErr = xmlDoc.parseError;
   alert("You have error " + myErr.reason);
} else {
   var result = Server.CreateObject("Msxml2.DOMDocument.5.0");
   // Now process the order and build the result document.
   Response.ContentType = "text/xml";
   result.save(Response);
%>
[Visual Basic]

Example

The following Microsoft Visual Basic® example creates an XMLHTTP object and asks a server for an XML document. The server sends back an XML document that is displayed by the code snippet.

Private Sub Command1_Click()
  Dim HttpReq As New MSXML2.XMLHTTP50
  
  HttpReq.open "GET", "http://XMLSampleServer/CatalogServer.asp", False
  HttpReq.send
  MsgBox HttpReq.responseText
End Sub
[C/C++]

Example

The following C/C++ example creates an XMLHTTP object and asks a server for an XML document. The server sends back an XML document that is displayed by the code snippet.

#import "msxml5.dll"
using namespace MSXML2;

void XMLHttpRequestSample()
{
   IXMLHTTPRequestPtr pIXMLHTTPRequest = NULL;
   BSTR bstrString = NULL;
   HRESULT hr;

   try {
      hr=pIXMLHTTPRequest.CreateInstance("Msxml2.XMLHTTP.5.0");
      SUCCEEDED(hr) ? 0 : throw hr;

      hr=pIXMLHTTPRequest->open("GET", "http://XMLSampleServer/CatalogServer.asp", false);
      SUCCEEDED(hr) ? 0 : throw hr;

      hr=pIXMLHTTPRequest->send();
      SUCCEEDED(hr) ? 0 : throw hr;

      bstrString=pIXMLHTTPRequest->responseText;

      MessageBox(NULL, _bstr_t(bstrString), _T("Results"), MB_OK);

      if(bstrString)
      {
         ::SysFreeString(bstrString);
         bstrString = NULL;
      }

   } catch (...) {
      MessageBox(NULL, _T("Exception occurred"), _T("Error"), MB_OK);
      if(bstrString)
         ::SysFreeString(bstrString);
   }

}

Remarks

A client computer can use the XMLHTTP object (MSXML2.XMLHTTP.5.0) to send an arbitrary HTTP request, receive the response, and have the Microsoft® XML Document Object Model (DOM) parse that response.

This object is integrated with Microsoft® XML Core Services (MSXML) 5.0 for Microsoft Office to support sending the request body directly from, and parsing the response directly into, the MSXML DOM objects. When combined with the support for Extensible Stylesheet Language (XSL), the XMLHTTP component provides an easy way to send structured queries to HTTP servers and efficiently display the results with a variety of presentations.

The usual sequence is to call the open method, set any custom header information through the setRequestHeader method followed by the send method, and then to check one of the four different response properties.

The XMLHTTP object is supported in Microsoft Internet Explorer 5.0 or later.

Versioning

MSXML 2.0 and later

Requirements

Implementation: msxml5.dll, msxml2.lib

[C/C++]

Header and IDL files: msxml2.h, msxml2.idl

Version-Dependent ProgID: Msxml2.XMLHTTP.5.0

Version-Dependent CLSID: 88D969C5-F192-11D4-A65F-0040963251E5

To view reference information for Visual Basic, C/C++, or Script only, click the Language Filter button Language Filter in the upper-left corner of the page.

See Also

IXMLHTTPRequest Members | IXMLDOMDocument/DOMDocument