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

前端之家收集整理的这篇文章主要介绍了xml – 经典asp中的System.Net.HttpWebRequest?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个经典的asp应用程序需要将XML发布到支付引擎,参考代码使用System.Net.HttpWebRequest对象(asp.net).我可以使用经典ASP中的等价物来发布XML吗?
这是我用来在ASP中发出HTTP请求的一个小帮助函数.它在JScript中,但你应该至少得到这个想法,以及我们多年来不得不解决的一些令人讨厌的问题的一些指示.
  1. <%
  2.  
  3. /*
  4. Class: HttpRequest
  5. Object encapsulates the process of making an HTTP Request.
  6.  
  7. Parameters:
  8. url - The gtarget url
  9. data - Any paramaters which are required by the request.
  10. method - Whether to send the request as POST or GET
  11. 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
  12.  
  13. Returns:
  14. Returns the result of the request in text format.
  15.  
  16. */
  17.  
  18. var HttpRequest = function( url,data,method,options )
  19. {
  20. options = options ? options : { "async" : false };
  21. options[ "async" ] = options["async"] ? true : false;
  22.  
  23. var text = "";
  24. data = data ? data : "";
  25. method = method ? String( method ).toUpperCase() : "POST";
  26.  
  27. // Make the request
  28. var objXmlHttp = new ActiveXObject( "MSXML2.ServerXMLHTTP" );
  29. objXmlHttp.setOption( 2,13056 ); // Ignore all SSL errors
  30.  
  31. try {
  32. objXmlHttp.open( method,url,options[ "async" ] ); // Method,URL,Async?
  33. }
  34. catch (e)
  35. {
  36. text = "Open operation Failed: " + e.description;
  37. }
  38.  
  39. objXmlHttp.setTimeouts( 30000,30000,30000 ); // Timeouts in ms for parts of communication: resolve,connect,send (per packet),receive (per packet)
  40. try {
  41. if ( method == "POST" ) {
  42. objXmlHttp.setRequestHeader( "Content-Type","application/x-www-form-urlencoded" );
  43. }
  44.  
  45. objXmlHttp.send( data );
  46.  
  47. if ( options[ "async" ] ) {
  48. return "";
  49. }
  50.  
  51. text = objXmlHttp.responseText;
  52.  
  53. } catch(e) {
  54. text = "Send data Failed: " + e.description;
  55. }
  56.  
  57. // Did we get a "200 OK" status?
  58. if ( objXmlHttp.status != 200 )
  59. {
  60. // Non-OK HTTP response
  61. text = "Http Error: " + objXmlHttp.Status + " " + Server.HtmlEncode(objXmlHttp.StatusText) + "\nFailed to grab page data from: " + url;
  62. }
  63.  
  64. objXmlHttp = null; // Be nice to the server
  65.  
  66. return text ;
  67. }
  68.  
  69. %>

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

  1. <%@ Language="JScript" %>
  2. <!--#include file="httprequest.asp"-->
  3. <%
  4.  
  5. var url = "http://www.google.co.uk/search";
  6. var data = "q=the+stone+roses"; // Notice you will need to url encode your values,simply pass them in as a name/value string
  7.  
  8. Response.Write( HttpRequest( url,"GET" ) );
  9.  
  10. %>

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

希望有所帮助.

猜你在找的XML相关文章