我有一个经典的asp应用程序需要将XML发布到支付引擎,参考代码使用System.Net.HttpWebRequest对象(asp.net).我可以使用经典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 ); // 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,"GET" ) );
- %>
一句警告,如果有错误,它将返回给您错误消息,无法捕获它.它可以满足我们的需求,如果我们需要更多的保护,那么我们可以创建一个可以更好地处理错误的自定义函数.
希望有所帮助.