System.Net.HttpWebRequest in classic asp?

时光怂恿深爱的人放手 提交于 2019-12-17 20:38:05

问题


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?


回答1:


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.




回答2:


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.




回答3:


I think the reason the Async version of this function works and avoids the "no send" error discussed here:

How do I fire an asynchronous call in asp classic and ignore the response?

Is that you're never freeing the COM object in the Async version - good that it fixes the problem, bad that it leaks big time resources.




回答4:


Everything AJAXy uses XMLHttp.
See if this link helps - http://www.mikesdotnetting.com/Article.aspx?ArticleID=39

EDIT: Don't accept this answer.
What I did is just search for it using google. Did you try that first?

I guess some of the questions can be answered by a search.
For everything else, there is StackOverflow.



来源:https://stackoverflow.com/questions/933553/system-net-httpwebrequest-in-classic-asp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!