views:

410

answers:

2

How would I go about creating a HTTP request with POST data in classic asp (not .net)? Google hasn't been much help

+3  A: 

You can try something like this:

Set ServerXmlHttp = Server.CreateObject("MSXML2.ServerXMLHTTP.6.0")
ServerXmlHttp.open "POST", "http://www.domain.com/page.asp"
ServerXmlHttp.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
ServerXlHttp.setRequestHeader "Content-Length", Len(PostData)
ServerXmlHttp.send PostData

If ServerXmlHttp.status = 200 Then
 TextResponse = ServerXmlHttp.responseText
 XMLResponse = ServerXmlHttp.responseXML
 StreamResponse = ServerXmlHttp.responseStream
Else
 ' Handle missing response or other errors here
End If

Set ServerXmlHttp = Nothing

where PostData is the data you want to post (eg name-value pairs, XML document or whatever).

You'll need to set the correct version of MSXML2.ServerXMLHTTP to match what you have installed.

The open method takes five arguments, of which only the first two are required:

ServerXmlHttp.open Method, URL, Async, User, Password
  • Method: "GET" or "POST"
  • URL: the URL you want to post to
  • Async: the default is False (the call doesn't return immediately) - set to True for an asynchronous call
  • User: the user name required for authentication
  • Password: the password required for authentication

When the call returns, the status property holds the HTTP status. A value of 200 means OK - 404 means not found, 500 means server error etc. (See http://en.wikipedia.org/wiki/List_of_HTTP_status_codes for other values.)

You can get the response as text (responseText property), XML (responseXML property) or a stream (responseStream property).

Simon Forrest
"You'll need to set the correct version of MSXML2.ServerXMLHTTP to match what you have installed." or just use MSXML2.ServerXMLHTTP.3.0 which is always present on all platforms currently in support.
AnthonyWJones
A: 

You must use one of the existing xmlhttp server objects directly or you could use a library which makes life a bit easier by abstracting the low level stuff away.

Check ajaxed implementation of fetching an URL

Disadvantage: You need to configure the library in order to make it work. Not sure if this is necessary for your project.

Michal