POSTing Form Data to a Web Page

ServerXMLHTTP Object

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.

You might also like...


About the author

Bruce Johnson Canada

I am the owner of a small application development consulting company that specialized in the design and implementation of Internet-based applications. While there are others who can make a web ...

Interested in writing for us? Find out more.


Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“The trouble with programmers is that you can never tell what a programmer is doing until it's too late.” - Seymour Cray