经典的asp中的System.Net.HttpWebRequest?

Cav*_*rob 4 xml post xmlhttprequest request asp-classic

我有一个经典的asp应用程序需要将XML发布到支付引擎,参考代码使用System.Net.HttpWebRequest对象(asp.net).我可以使用经典ASP中的等价物来发布XML吗?

Pet*_*son 6

这是我用于在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 ;
}

%>
Run Code Online (Sandbox Code Playgroud)

如果将其保存在文件(称为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" ) );

%>
Run Code Online (Sandbox Code Playgroud)

一句警告,如果它有错误,它将返回给你错误信息,无法捕捉它.它可以满足我们的需求,如果我们需要更多的保护,那么我们可以创建一个可以更好地处理错误的自定义函数.

希望有所帮助.