Using Microsoft's XMLHTTP Object to Get Data From Other Web Pages, Part 2
By Richard Lowe
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".
|
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 |
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.
|
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!
Richard Lowe works as a Development Services consultant for Spherion in the Technology Group
Related Links: