我有一个经典的asp应用程序,它需要将XML发布到支付引擎,并且参考代码使用了System.Net.HttpWebRequest对象(asp.net)。在Classic ASP中有没有我可以用来发布XML的等价物?
发布于 2009-06-01 13:23:35
下面是我在ASP中用来发出HTTP请求的一个小帮手函数。它是在JScript中实现的,但您至少应该了解一些想法,以及一些多年来我们必须解决的令人讨厌的陷阱。
<%
/*
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 ;
}
%>如果您将其保存在一个名为httprequest.asp的文件中,则可以使用以下代码来使用它:
<%@ 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" ) );
%>一句警告,如果它有一个错误,它将返回给你错误消息,没有办法捕获它。它可以很好地满足我们的需求,如果我们需要更多的保护,那么我们可以创建一个自定义函数,它可以更好地处理错误。
希望这能有所帮助。
发布于 2009-06-01 06:03:42
传统的ASP可以使用XMLHTTP ActiveX对象或通过MSXML库提供的ServerXMLHTTP对象来发起请求。(MSDN reference.
This KB article提供了ServerXMLHTTP对象的一个很好的参考和示例代码。
发布于 2011-02-23 07:06:09
我认为这个函数的异步版本之所以有效,并避免了这里讨论的“不发送”错误:
How do I fire an asynchronous call in asp classic and ignore the response?
你永远不会释放异步版本中的COM对象--好的是它解决了这个问题,坏的是它泄漏了大量的时间资源。
https://stackoverflow.com/questions/933553
复制相似问题