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?
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With