在传统的ASP System.Net.HttpWebRequest? [英] System.Net.HttpWebRequest in classic asp?

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

问题描述

我有需要发布的XML付款发动机传统的ASP应用程序,并参考code使用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) + "\nFailed to grab page data from: " + url;
    }

    objXmlHttp = null; // Be nice to the server

    return  text ;
}

%>

如果您保存在一个文件中(称为HTT prequest.asp)的,您可以使用此code使用它:

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天全站免登陆