我有一个经典的ASP应用程序,需要向付款引擎提交XML,参考代码使用System.Net.HttpWebRequest对象(asp.net)。在Classic ASP中是否有类似的对象可用于提交XML?
我有一个经典的ASP应用程序,需要向付款引擎提交XML,参考代码使用System.Net.HttpWebRequest对象(asp.net)。在Classic ASP中是否有类似的对象可用于提交XML?
这是我在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 ;
}
%>
<%@ 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" ) );
%>
需要注意的是,如果出现错误,它将返回错误消息,无法捕获。对于我们的需求来说,它表现得很好,如果我们需要更多的保护,那么我们可以创建一个自定义函数来更好地处理错误。
希望这能有所帮助。
经典 ASP 可以使用 XMLHTTP
ActiveX 对象或者通过 MSXML 库提供的 ServerXMLHTTP
对象来发起请求。(MSDN reference)。
这篇 KB 文章 提供了一个很好的关于 ServerXMLHTTP
对象的参考和示例代码。
所有涉及 AJAX 的东西都使用 XMLHttp。
看看这个链接是否有帮助 - http://www.mikesdotnetting.com/Article.aspx?ArticleID=39
编辑:不要接受这个答案。
我所做的就是用谷歌搜索它。你先试过了吗?
我想一些问题可以通过搜索得到答案。
对于其他所有问题,都可以在 StackOverflow 上找到答案。