Published: Wednesday, November 01, 2000
Using Microsoft's XMLHTTP Object to Get Data From Other Web Pages, Part 2
By Richard Lowe
Read Part 1
In Part 1 we examined how to use XMLHTTP to get simple HTML and
binary data. In this part we'll look at how to POST data and how to request secured pages via XMLHTTP!
Posting Data
It's also very easy to do POST requests to remote web sites. This example searches the Internet Movie
Database for "The Usual Suspects".
<%
Response.Buffer = True
Dim objXMLHTTP, xml
Set xml = Server.CreateObject("Microsoft.XMLHTTP")
' Notice the two changes in the next two lines:
xml.Open "POST", "http://www.imdb.com/Find", False
xml.Send "select=All&for=The Usual Suspects"
Response.Write xml.responseText
Set xml = Nothing
%>
|
[
View the live demo!]
The Open method takes the argument POST instead of GET and the
Send method contains the POST data. POST
data has exactly the same syntax as query string data, only you don't need to include the ? at
the beginning of the string. The only way to know what to post is to examine the form doing the posting, and
see what the elements are and what sort of values they submit.
Setting the Content-Type |
January 7th, 2006: Alert 4Guys reader Dan writes in to add:
When you post form data using the XMLHTTP component [if] you don't set the content-type header,
... form data to be unreadable by the target script. The
following line needs to be added after the xml.open line for the target
script to be able to access the data through the Request.Form collection:
xml.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
|
Accessing Secure Sites
XMLHttp can also access data that requires authentication. The Open method also accepts two
optional arguments for a user name and password, as seen in the example below. Note that this user name and
password only applies to web sites that request credentials (using the WWW-Authenticate header) like IIS
does when you have Basic and/or challenge-response authentication enabled. For sites that use a
database-style authentication (like Hotmail or Yahoo mail) you should simply post the user name and
password like you would post any other data.
<%
Response.Buffer = True
Dim objXMLHTTP, xml
Set xml = Server.CreateObject("Microsoft.XMLHTTP")
xml.Open "GET", "http://www.4Guysfromrolla.com/", False, _
"Richard", "Welcome"
' Actually Sends the request and returns the data:
xml.Send
Response.Write xml.responseText
Set xml = Nothing
%>
|
There are other methods and properties of the Microsoft XMLHttp component, but the vast majority of uses only
requires the ones discussed in the examples above.
Happy Programming!
By Richard Lowe
Richard Lowe works as a Development Services consultant for Spherion in the
Technology Group
Related Links:
Download the MSXML 3.0 Beta Preview
Read Grabbing Information from other Servers