Discuz教程网

SmartHTTP 简易HttpRequest类(ASP)

[复制链接]
authicon dly 发表于 2011-9-8 20:18:19 | 显示全部楼层 |阅读模式
最简单的调用方法:
response.write SmartHttp("http://www.baidu.com/").send().gettext()

复杂调用
set myhttp = SmartHttp("http://www.baidu.com/s","GET")
myhttp.dataset.append "wd","smarthttp"
myhttp.send()
response.write myhttp.gettext("gbk")


  1. <script language="jscript" runat="server">
  2. function SmartHttp(url,method,data){
  3. return new _SmartHttp(url,method,data);
  4. }

  5. function _SmartHttp(url,method,data){
  6. if(typeof method=="undefined") method="GET";
  7. if(typeof data=="undefined") data="";
  8. method = method.toUpperCase();
  9. method = method!="POST" ? "GET" : "POST";

  10. this.method = method;
  11. this.url=url;
  12. this.data=data;
  13. this.charset="gb2312";
  14. this.http=null;
  15. this.headers=[];
  16. this.status=0;
  17. this.readyState=0;
  18. this.content=null;
  19. this.msg="";
  20. this.dataset={
  21. charset:"gb2312",
  22. data:[],
  23. append:function(key,value,noencode){
  24. var fn=null;
  25. if(this.charset.toLowerCase()=="utf-8"){fn = encodeURIComponent;}else{fn = escape;}
  26. if(noencode==true){fn=function(_str){return _str;}}
  27. this.data.push({"key":fn(key),"value":fn(value)});
  28. },
  29. remove:function(key){
  30. if(this.data.length<=0) return false;
  31. var _data=[];
  32. for(var i=0;i<this.data.length;i++){
  33. if(this.data[i].key!=key){
  34. _data.push(this.data[i]);
  35. }
  36. }
  37. this.data = _data;
  38. },
  39. isexists:function(key){
  40. if(this.data.length<=0) return false;
  41. for(var i=0;i<this.data.length;i++){
  42. if(this.data[i].key==key){
  43. return true;
  44. }
  45. }
  46. return false;
  47. },
  48. clear:function(){
  49. this.dataset.data=[];
  50. }
  51. };
  52. }

  53. _SmartHttp.prototype.init=function(){
  54. var datasetstr="";
  55. if(this.dataset.data.length>0){
  56. for(var i=0;i<this.dataset.data.length;i++){
  57. datasetstr += this.dataset.data[i].key + "=" + this.dataset.data[i].value + "&";
  58. }
  59. }
  60. if(datasetstr!="") datasetstr = datasetstr.substr(0,datasetstr.length-1);
  61. if(this.data==""){this.data = datasetstr;}else{if(datasetstr!="")this.data+= "&" + datasetstr;}
  62. if(this.data=="")this.data=null;
  63. this.url += ((this.url.indexOf("?")<0) ? "?" : "&") + "jornd=" + this.getrnd();
  64. if(this.method=="GET" && this.data!=null) this.url += "&" + this.data;
  65. if(this.method=="POST") this.headers.push("Content-Type:application/x-www-form-urlencoded");
  66. if(!this.charset || this.charset=="") this.charset = "gb2312";
  67. };

  68. _SmartHttp.prototype.header=function(headstr){
  69. if(headstr.indexOf(":")>=0) this.headers.push(headstr);
  70. };
  71. _SmartHttp.prototype.send=function(){
  72. this.init();
  73. var _http = this.getobj();
  74. if(_http==null){return "";}
  75. try{_http.setTimeouts(10000,10000,10000,30000);}catch(ex){}
  76. _http.open(this.method,this.url,false);
  77. if(this.headers.length>0){
  78. for(var i=0;i<this.headers.length;i++){
  79. var Sindex = this.headers[i].indexOf(":");
  80. var key = this.headers[i].substr(0,Sindex);
  81. var value = this.headers[i].substr(Sindex+1);
  82. _http.setRequestHeader(key,value);
  83. }
  84. }
  85. _http.send(this.data);
  86. this.readyState = _http.readyState;
  87. if(_http.readyState==4){
  88. this.status = _http.status;
  89. this.http = _http;
  90. this.content = _http.responseBody;
  91. }
  92. return this;
  93. }

  94. _SmartHttp.prototype.getbinary=function(){
  95. return this.content;
  96. };

  97. _SmartHttp.prototype.gettext=function(charset){
  98. try{
  99. return this.b2s(this.content,charset ? charset : this.charset);
  100. }catch(ex){
  101. this.msg = ex.description;
  102. return "";
  103. }
  104. };

  105. _SmartHttp.prototype.getjson=function(charset){
  106. try{
  107. var _json=null;
  108. eval("_json=(" + this.gettext(charset) + ");");
  109. return _json;
  110. }catch(ex){
  111. this.msg = ex.description;
  112. return null;
  113. }
  114. };

  115. _SmartHttp.prototype.getxml=function(charset){
  116. try{
  117. var _dom = new ActiveXObject("MSXML2.DOMDocument");
  118. _dom.loadXML(this.gettext(charset).replace("&","&"));
  119. return _dom;
  120. }catch(ex){
  121. this.msg = ex.description;
  122. return null;
  123. }
  124. };
  125. _SmartHttp.prototype.getobj = function (){
  126. var b=null;
  127. var httplist = ["MSXML2.serverXMLHttp.3.0","MSXML2.serverXMLHttp","MSXML2.XMLHttp.3.0","MSXML2.XMLHttp","Microsoft.XMLHttp"];
  128. for(var i = 0;i<=httplist.length -1;i++){
  129. try{
  130. b= new ActiveXObject(httplist[i]);
  131. (function(o){
  132. _SmartHttp.prototype.getobj = function(){return new ActiveXObject(o)};
  133. })(httplist[i]);
  134. return b;
  135. }catch(ex){
  136. //eval("this.msg = ex.description;");
  137. }
  138. }
  139. return b;
  140. };

  141. _SmartHttp.prototype.getrnd = function (){return Math.random().toString().substr(2);};

  142. _SmartHttp.prototype.b2s = function(bytSource, Cset){
  143. var Objstream;
  144. var byts;
  145. Objstream =Server.CreateObject("ADODB.Stream");
  146. Objstream.Type = 1;
  147. Objstream.Mode = 3;
  148. Objstream.Open();
  149. Objstream.Write(bytSource);
  150. Objstream.Position = 0;
  151. Objstream.Type = 2;
  152. Objstream.CharSet = Cset;
  153. byts = Objstream.ReadText();
  154. Objstream.Close();
  155. Objstream = null;
  156. return byts;
  157. };
  158. _SmartHttp.prototype.urlencode=function(str){ return encodeURIComponent(str);};
  159. _SmartHttp.prototype.urldecode=function(str){ return decodeURIComponent(str);};
  160. </script>
复制代码




上一篇:为SWFUpload增加ASP版本的上传处理程序
下一篇:用SQL批量插入数据的存储过程
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

1314学习网 ( 浙ICP备10214163号 )

GMT+8, 2025-5-2 08:50

Powered by Discuz! X3.4

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表