I've got a classic asp app that needs to post XML to a payment engine, and the reference code uses a System.Net.HttpWebRequest object (asp.net). Is there an equivalent in Classic ASP that I could use to post the XML?
4 Answers
Heres a little helper function I use for making HTTP requests in ASP. Its in JScript but you should get the idea at least and some pointers of some nasty gotcha's that we've had to iron out over the years.
<%
/*
Class: HttpRequest
Object encapsulates the process of making an HTTP Request.
Parameters:
url - The gtarget url
data - Any paramaters which are required by the request.
method - Whether to send the request as POST or GET
options - async (true|false): should we send this asyncronously (fire and forget) or should we wait and return the data we get back? Default is false
Returns:
Returns the result of the request in text format.
*/
var HttpRequest = function( url, data, method, options )
{
options = options ? options : { "async" : false };
options[ "async" ] = options["async"] ? true : false;
var text = "";
data = data ? data : "";
method = method ? String( method ).toUpperCase() : "POST";
// Make the request
var objXmlHttp = new ActiveXObject( "MSXML2.ServerXMLHTTP" );
objXmlHttp.setOption( 2, 13056 ); // Ignore all SSL errors
try {
objXmlHttp.open( method, url, options[ "async" ] ); // Method, URL, Async?
}
catch (e)
{
text = "Open operation failed: " + e.description;
}
objXmlHttp.setTimeouts( 30000, 30000, 30000, 30000 ); // Timeouts in ms for parts of communication: resolve, connect, send (per packet), receive (per packet)
try {
if ( method == "POST" ) {
objXmlHttp.setRequestHeader( "Content-Type", "application/x-www-form-urlencoded" );
}
objXmlHttp.send( data );
if ( options[ "async" ] ) {
return "";
}
text = objXmlHttp.responseText;
} catch(e) {
text = "Send data failed: " + e.description;
}
// Did we get a "200 OK" status?
if ( objXmlHttp.status != 200 )
{
// Non-OK HTTP response
text = "Http Error: " + objXmlHttp.Status + " " + Server.HtmlEncode(objXmlHttp.StatusText) + "\nFailed to grab page data from: " + url;
}
objXmlHttp = null; // Be nice to the server
return text ;
}
%>
If you save that in a file (called httprequest.asp) the you can use it using this code:
<%@ Language="JScript" %>
<!--#include file="httprequest.asp"-->
<%
var url = "http://www.google.co.uk/search";
var data = "q=the+stone+roses"; // Notice you will need to url encode your values, simply pass them in as a name/value string
Response.Write( HttpRequest( url, data, "GET" ) );
%>
One word of warning, if it has an error it will return to you the error message, no way of catching it. It does fine for our needs, if we need a bit more protection then we can create a custom function which can handle the errors a bit better.
Hope that helps.

- 3,208
- 2
- 25
- 35
-
+1, Its a nice little function but it does assume that a post is emulating a HTML Form post. I'd remove that assumption and make the content type header an optional property on the options object. You could default it but allow the external code to specify it such as "text/xml". – AnthonyWJones Jun 02 '09 at 07:39
-
Too true Anthony, its one of those functions that grew out of need at the time and fits our needs for now. One of the reason I love using JScript for ASP is that you can learn from all of the client-side goodies and techniquies that have come about in recent years. By passing an options object through it would be easy to refactor to allow for a "contenttype" option to be passed through while still not breaking anything that currently uses it. Nice point though, I might look to adding it in to our code base ;) – Pete Duncanson Jun 02 '09 at 16:30
Classic ASP can use the XMLHTTP
ActiveX object or the ServerXMLHTTP
object available via the MSXML library to initiate requests. (MSDN reference).
This KB article provides a good reference and example code of the ServerXMLHTTP
object.

- 25,615
- 8
- 56
- 70
-
1The ServerXMLHTTP is the option to use here, XMLHTTP should not be used server-side, it isn't thread-safe. – AnthonyWJones Jun 02 '09 at 07:41
I think the reason the Async version of this function works and avoids the "no send" error discussed here:
How do I fire an asynchronous call in asp classic and ignore the response?
Is that you're never freeing the COM object in the Async version - good that it fixes the problem, bad that it leaks big time resources.
Everything AJAXy uses XMLHttp.
See if this link helps - http://www.mikesdotnetting.com/Article.aspx?ArticleID=39
EDIT: Don't accept this answer.
What I did is just search for it using google. Did you try that first?
I guess some of the questions can be answered by a search.
For everything else, there is StackOverflow.

- 33,172
- 3
- 63
- 88