As of MSXML 3.0, the XMLHTTP object was added as a means of submitting HTTP requests to a web site. With MSXML 4.0, the ServerXMLHTTP object was added to accomplish the same function from the server side of a web site. The interface to both objects is same (at least at the level that we'll be dealing with them) and makes it quite easy to send requests to a web page.
The trick to emulating a form POST is in knowing how to structure the request. It actually looks suspiciously like the data portion of a query string. The difference is that the data needs to be placed in the body of the request instead of on the URL.
Dim xmlhttp As Object
Set xmlhttp = CreateObject("MSXML2.ServerXMLHTTP")
' Indicate that page that will receive the request and the
' type of request being submitted
xmlhttp.Open "POST", "http://localserver/test.asp", False
' Indicate that the body of the request contains form data
xmlhttp.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
' Send the data as name/value pairs
xmlhttp.send "Id=1&S=2"
Set xmlhttp = Nothing
Setting the content-type allows any special characters to be converted to their URL Encoded equivilent. The results from the form POST can be viewed in one of two properties. The ReponseText property contains the raw HTML code the is returned from the page. The ResponseXML property is an XML document containing the sameinformation. Assuming that the MIME type was set to "text/xml" before being returned. Otherwise, ResponseXML will be empty. Also, you should be aware that, if the returned XML is not well formed, the ResponseXML.XML property will be empty. As with the regular MSXML documents, you can check the parseError object to determine why that is.
Comments