经典asp中的System.Net.HttpWebRequest? [英] System.Net.HttpWebRequest in classic asp?

查看:16
本文介绍了经典asp中的System.Net.HttpWebRequest?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

推荐答案

这里有一个小辅助函数,我用来在 ASP 中发出 HTTP 请求.它在 JScript 中,但您至少应该了解一些想法,并指出我们多年来不得不解决的一些令人讨厌的问题.

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) + "
Failed to grab page data from: " + url;
    }

    objXmlHttp = null; // Be nice to the server

    return  text ;
}

%>

如果您将其保存在一个文件(称为 httprequest.asp)中,您可以使用以下代码使用它:

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.

希望有所帮助.

这篇关于经典asp中的System.Net.HttpWebRequest?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆